Java 串交替字母
我正在尝试编写一段代码,向用户请求两个字符串。如何编写代码,通过交替两个字符串的字符形成一个新字符串 感谢您的帮助简单的“愚蠢”方法:)Java 串交替字母,java,Java,我正在尝试编写一段代码,向用户请求两个字符串。如何编写代码,通过交替两个字符串的字符形成一个新字符串 感谢您的帮助简单的“愚蠢”方法:) 类字符串合并 { 公共静态字符串合并(字符串a、字符串b) { 如果(a==null | | a.length()==0){返回b;} else如果(b==null | | b.length()==0){返回a;} 其他的 { StringBuffer merged=新的StringBuffer(); intaindex=0; int-bIndex=0; 而(
类字符串合并
{
公共静态字符串合并(字符串a、字符串b)
{
如果(a==null | | a.length()==0){返回b;}
else如果(b==null | | b.length()==0){返回a;}
其他的
{
StringBuffer merged=新的StringBuffer();
intaindex=0;
int-bIndex=0;
而(aIndex
假设用户输入两个长度相同的字符串:
索引%2==0
,则从用户输入的第二个字符串中提取字符,并将其添加到空字符串中mohaps方法的一个稍短、稍快的版本(由于StringBuilder):
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();
}
}
}
类字符串合并{
公共静态字符串合并(最终字符串a、最终字符串b){
如果(a==null | | a.length()==0){
返回b;
}else if(b==null | | b.length()==0){
返回a;
}否则{
最终长度=a.长度();
最终整数长度=b.长度();
最终合并的StringBuilder=新的StringBuilder(aLength+bLength);
对于(inti=0,j=0;ibLength){
合并。追加(a.substring(bLength));
}否则{
合并。追加(b.substring(aLength));
}
}
返回merged.toString();
}
}
}
编辑:在创建StringBuilder实例时添加长度与mohaps和shams(微笑)基本相同,但使用数组:
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);
}
输出:
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
这是家庭作业吗?另外,两个字符串的长度相等吗?当一个字符串比另一个长时会发生什么情况?不,字符串的长度不相等……如果一个字符串比另一个长,它应该打印最长字符串的结尾。这真的很有帮助……我还没有真正学习数组,所以一路上都不理解yanicks。@John——随着你成为程序员,传递一些东西会对你有所帮助。始终尝试使用您所知道的从“无解决方案”到“解决方案”,然后尝试优化它。请不要在这句话中读到任何讽刺或傲慢,没有任何意图:)我总是告诉开发人员,我在我的团队中指导开发人员,并且我个人发现这是一种提高自己技能和理解+1使用substring()的好方法对于较大字符串的其余部分:)+1对于预先确定的大小和就地建筑:)我的代码示例更侧重于演示该方法(以及健全性检查):P
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