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