[英]Making part of string equal to other part of string
// Convert a string to Robber Speak
public static String toRobber(String text) {
String vowel = "";
String robber = "";
for (int i = 0; i <= text.length(); i++) {
{
if (!isVowel(text.charAt(i))) {
vowel = text.charAt(i) + "o" + text.charAt(i);
robber = vowel + text.substring(i+1);
i++;
}
else {
robber.substring(i,i) = text.substring(i,i);
}
}
}
return robber;
}
In the else statement (line 10) java demands a variable.在 else 语句(第 10 行)中,java 需要一个变量。 How do i set part of a string equal to another part of a string?
如何将字符串的一部分设置为字符串的另一部分?
A method call such as robber.substring(i,i)
cannot be on the left hand side of an assignment operator.诸如
robber.substring(i,i)
类的方法调用不能位于赋值运算符的左侧。
Besides, String
s are immutable, so you'll have to create a new String
.此外,
String
是不可变的,因此您必须创建一个新String
。
For example:例如:
char[] chars = robber.toCharArray();
chars[i] = text.charAt(i);
robber = new String (chars);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.