Java 将字符串中的每个字母加倍
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20052445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doubling each letter in a String
提问by TaylorTDHouse
I'm doing a project for Java 1, and I'm completely stuck on this question.
我正在为 Java 1 做一个项目,我完全被这个问题困住了。
Basically I need to double each letter in a string.
基本上我需要将字符串中的每个字母加倍。
"abc" -> "aabbcc"
"uk" -> "uukk"
"t" -> "tt"
I need to do it in a while loop in what is considered "Java 1" worthy. So i'm guessing that this means more of a problematic approach.
我需要在一个被认为是“Java 1”的循环中完成它。所以我猜这意味着更多的问题方法。
I know that the easiest way for me to do this, from my knowledge, would be using the charAt method in a while loop, but for some reason my mind can't figure out how to return the characters to another method as a string.
我知道,据我所知,最简单的方法是在 while 循环中使用 charAt 方法,但由于某种原因,我无法弄清楚如何将字符作为字符串返回给另一个方法。
Thanks
谢谢
[EDIT] My Code (wrong, but maybe this will help)
[编辑] 我的代码(错误,但也许这会有所帮助)
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index);
index++;
}
采纳答案by thanksd
Yeah, a for loop would really make more sense here, but if you need to use a while loop then it would look like this:
是的,for 循环在这里确实更有意义,但是如果您需要使用 while 循环,则它看起来像这样:
String s = "abc";
String result = "";
int i = 0;
while (i < s.length()){
char c = s.charAt(i);
result = result + c + c;
i++;
}
回答by luanjot
You can do:
你可以做:
public void doubleString(String input) {
String output = "";
for (char c : input.toCharArray()) {
output += c + c;
}
System.out.println(output);
}
回答by zenbeni
public static char[] doubleChars(final char[] input) {
final char[] output = new char[input.length * 2];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
output[i + 1] = input[i];
}
return output;
}
回答by subash
try this
尝试这个
String a = "abcd";
char[] aa = new char[a.length() * 2];
for(int i = 0, j = 0; j< a.length(); i+=2, j++){
aa[i] = a.charAt(j);
aa[i+1]= a.charAt(j);
}
System.out.println(aa);
回答by SimplyPanda
Your intuition is very good. charAt(i)
will return the character in the string at location i
, yes?
你的直觉非常好。charAt(i)
将返回字符串中位置的字符i
,是吗?
You also said you wanted to use a loop. A for
loop, traversing the length of the list, string.length()
, will allow you to do this. At every single node in the string, what do you need to do? Double the character.
你还说你想使用循环。一个for
循环,遍历列表的长度,string.length()
将允许你这样做。在字符串中的每个节点上,您需要做什么?字符加倍。
Let's take a look at your code:
让我们来看看你的代码:
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index); //return ends the method
index++;
}
Problematically for your code, you are returning two characters immediately upon entering the loop. So for a string abc
, you are returning aa
. Let's store the aa
in memory instead, and then return the completed string like so:
对于您的代码有问题,您在进入循环后立即返回两个字符。因此,对于字符串abc
,您正在返回aa
。让我们将其存储aa
在内存中,然后像这样返回完成的字符串:
int index = 0;
int length = str.length();
String newString = "";
while (index < length) {
newString += str.charAt(index) + str.charAt(index);
index++;
}
return newString;
This will add the character to newString
, allowing you to return the entire completed string, as opposed to a single set of doubled characters.
这会将字符添加到newString
,允许您返回整个完整的字符串,而不是一组双倍字符。
By the way, this may be easier to do as a for loop, condensing and clarifying your code. My personal solution (for a Java 1 class) would look something like this:
顺便说一句,这可能更容易作为 for 循环来执行,从而压缩和澄清您的代码。我的个人解决方案(针对 Java 1 类)如下所示:
String newString = "";
for (int i = 0; i < str.length(); i++){
newString += str.charAt(i) + str.charAt(i);
}
return newString;
Hope this helps.
希望这可以帮助。
回答by Ankit Rustagi
Assuming this is inside a method, you should understand that you can only return oncefrom a method. After encountering a return statement, the control goes back to the calling method. Thus your approach of returning char every time in a loop is faulty.
假设这是在一个方法中,你应该明白你只能从一个方法返回一次。遇到 return 语句后,控制返回到调用方法。因此,您每次在循环中返回 char 的方法是错误的。
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index); // only the first return is reachable,other are not executed
index++;
}
Change your method to build a String and return it
更改您的方法以构建一个字符串并返回它
public String modify(String str)
{
int index = 0;
int length = str.length();
String result="";
while (index < length) {
result += str.charAt[index]+str.charAt[index];
index++;
}
return result;
}
回答by Holger
String s="mystring".replaceAll(".", "##代码####代码##");
The method String.replaceAll
uses the regular expression syntax which is described in the documentation of the Pattern
class, where we can learn that .
matches “any character”. Within the replacement, $numberrefers to numbered “capturing group” whereas $0
is predefined as the entire match. So $0$0
refers to the matching character two times. As the name of the method suggests, it is performed for all matches, i.e. all characters.
该方法String.replaceAll
使用类的文档中Pattern
描述的正则表达式语法,我们可以在其中学习.
匹配“任何字符”。在替换中,$number指代编号的“捕获组”,而$0
预定义为整个匹配项。So$0$0
两次引用匹配的字符。正如该方法的名称所暗示的那样,它针对所有匹配项(即所有字符)执行。