Q
字符串交替字母
2
A
回答
2
簡單的 「啞」 的做法:)
class StringMerge
{
public static String merge(String a, String b)
{
if (a == null || a.length() == 0){ return b; }
else if(b == null || b.length() == 0){ return a; }
else
{
StringBuffer merged = new StringBuffer();
int aIndex = 0;
int bIndex = 0;
while(aIndex < a.length() && bIndex < b.length())
{
merged.append(a.charAt(aIndex));
merged.append(b.charAt(bIndex));
aIndex++;
bIndex++;
}
while(aIndex < a.length())
{
merged.append(a.charAt(aIndex));
aIndex++;
}
while(bIndex < b.length())
{
merged.append(b.charAt(bIndex));
bIndex++;
}
return merged.toString();
}
}
}
0
假設用戶輸入相同長度的兩個字符串:
- 建立一個新的,空的字符串。
- 循環...
- 如果
Index % 2 == 0
,請從用戶輸入的第二個字符串中取出字符,並將其添加到空字符串中。 - 否則,從第一個字符串中取出字符並將其添加到空字符串中。
- 當沒有更多字符添加時停止循環。
2
甲稍短和更快[由於StringBuilder的]的mohaps'的方法版本:
class StringMerge {
public static String merge(final String a, final String b) {
if (a == null || a.length() == 0) {
return b;
} else if (b == null || b.length() == 0) {
return a;
} else {
final int aLength = a.length();
final int bLength = b.length();
final StringBuilder merged = new StringBuilder(aLength + bLength);
for (int i = 0, j = 0; i < aLength && j < bLength; i++, j++) {
merged.append(a.charAt(i)).append(b.charAt(j));
}
if (aLength != bLength) {
if (aLength > bLength) {
merged.append(a.substring(bLength));
} else {
merged.append(b.substring(aLength));
}
}
return merged.toString();
}
}
}
編輯:添加長度,同時創造StringBuilder實例
1
差不多相同mohaps和枕套( 微笑),但使用的數組:
static public void main(String...args) {
Scanner scanner = new Scanner(System.in);
System.out.print("String 1 : ");
String s1 = scanner.nextLine();
System.out.print("String 2 : ");
String s2 = scanner.nextLine();
System.out.println("Combined string is : " + mergeStrings(s1, s2));
}
static public String mergeStrings(String a, String b) {
if (a == null) a = "";
if (b == null) b = "";
char[] chars = new char[a.length() + b.length()];
int index = 0, ia = 0, ib = 0;
while (ia<a.length() && ib<b.length()) {
chars[index++] = a.charAt(ia++);
chars[index++] = b.charAt(ib++);
}
while (ia<a.length()) {
chars[index++] = a.charAt(ia++);
}
while (ib<b.length()) {
chars[index++] = b.charAt(ib++);
}
return new String(chars);
}
** UPDATE **
稍微改進,加入一個start
位置(默認爲0
)開始在從a
特定位置合併。如果start
爲負數,則該方法的行爲如同0
。如果start
大於字符串a
的長度,則字符串將用空格填充,直到達到start
。
static public String mergeStrings(String a, String b) {
return mergeStrings(a, b, 0);
}
static public String mergeStrings(String a, String b, int start) {
if (a == null) a = "";
if (b == null) b = "";
int len = Math.max(start - a.length(), 0) + a.length() + b.length();
char[] chars = new char[len];
int index = 0, ia = 0, ib = 0;
while (ia<a.length() && ia<start) {
chars[index++] = a.charAt(ia++);
}
while (index<start) {
chars[index++] = ' ';
}
while (ia<a.length() && ib<b.length()) {
chars[index++] = a.charAt(ia++);
chars[index++] = b.charAt(ib++);
}
while (ia<a.length()) {
chars[index++] = a.charAt(ia++);
}
while (ib<b.length()) {
chars[index++] = b.charAt(ib++);
}
return new String(chars);
}
輸出:
String 1 : hello
String 2 : world
Combined string is : hweolrllod
Combined merged at 2 : helwloorld
Combined merged at 4 : helloworld
Combined merged at 10 : hello world
+0
對於預先確定的大小和就地建立+1)我的代碼示例更側重於演示方法(以及理智檢查):P – mohaps 2011-03-06 02:49:52
相關問題
- 1. 字符串中的字母替換
- 2. 如果字符串中的字母替換爲字符串中的字母
- 3. 如何以字母順序替換字符串中的字母
- 4. 用PHP中的第n個字母字符替換字符串字母
- 5. 用字符替換字符串中的字母
- 6. 字母數字字符串
- 7. 如果字符串存在,用提交的字符串替換字符串
- 8. 字符串放下字母?
- 9. 返回不同長度的兩個字符串中的交替字母
- 10. 字母字符檢查字符串
- 11. 如何返回由兩個字符串交替字符組成的字符串?
- 12. 字符串字符交換
- 13. 如何用另一個字母替換字符串中的字母
- 14. 將所有大寫字母替換爲小寫字母,除了urlencoded字符串
- 15. 對字母數字字符串進行排序,然後替代數字/字母字符輸出
- 16. 如何將字符串中的數字替換/替換爲字符串內字母的乘數?
- 17. 用T-SQL中的字符和數字替換字符串中的字母
- 18. 用字符串中的隨機字符替換字母和數字
- 19. 替換字符串內的字符串
- 20. 字符串中的字符串替換
- 21. 將字符串替換爲字符串
- 22. 交替向字符串的開頭和結尾添加字符
- 23. 如何從PowerShell中的字符串中獲取交替字符?
- 24. 字符串交替字符方法的輸出
- 25. 如何將字符串拆分爲字母字符串和數字字符串?
- 26. 如何用下劃線替換字符串的字母?
- 27. 替換字符串中的多個字母
- 28. 替換字符串數組中的最後一個字母Javascript
- 29. 用R中的一個字母替換整個字符串
- 30. VB 2010 - 用破折號代替字符串的所有字母
是這個家庭作業?還有,兩個琴絃的長度是否相等?當一個字符串比另一個更長時會發生什麼? – 2011-03-06 02:14:42
沒有字符串的長度不相等...如果一個比另一個長,它應該打印最長的一個末尾 – John 2011-03-06 02:35:27