我试图编写一段代码,用来向用户请求两个字符串。我如何编写代码,以便通过交替两个字符串的字符形成一个新的字符串。
感谢任何帮助
答案 0 :(得分: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();
}
}
}
答案 1 :(得分: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实例时添加长度
答案 2 :(得分:1)
与mohaps和shams( smile )几乎相同,但是使用数组:
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);
}
** 更新 **
略有改进,添加了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
答案 3 :(得分:0)
假设用户输入两个长度相同的字符串:
Index % 2 == 0
,请从用户输入的第二个字符串中取出字符,并将其添加到空字符串中。