[英]What is wrong in using charAt function in this context?
I was trying to copy a word using this manner. 我试图用这种方式复制一个单词。 I am not sure,I was following right manner handling String
. 我不确定,我是否按照正确的方式处理String
。
The code is: 代码是:
public static void main(String args[])
{
String str="Hello";
int i=0;
String copy = "";
while (str.charAt(i) !='\0')
{
copy = copy + str.charAt(i);
i++;
}
System.out.println(copy);
}
Running this code results in Exception
: 运行此代码将导致Exception
:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(Unknown Source)
at ReverseWord.main(ReverseWord.java:15)
Am I using charAt()
and checking null in right way? 我在使用charAt()
并以正确的方式检查null吗? Or,I have wrong conception about String
handling in Java
? 或者,我对Java
String
处理有错误的观念?
You are using String
s in the wrong way (for Java
!) lets clarify some basic points to use String's in Java: 您以错误的方式使用String
(对于Java
!)让我们澄清一些在Java中使用String的基本要点:
String
are immutable . String
是不可变的 。 This means each time you modify it JVM
will create a new object. 这意味着每次修改时, JVM
都会创建一个新对象。 That is a lot of resources, so, in order of better programming you shouldn't use concatenation in String
s, to make concatenation use StringBuilder
. 这是很多资源,因此,为了更好地编程,您不应在String
使用串联,而应使用StringBuilder
进行串联。 Strings
does not end with any special symbol , this could happen in some filetypes but not at Strings
objects, so you have to get size with length()
and use it to iterate if necessary. Strings
不以任何特殊符号结尾 ,这可能在某些文件类型中发生,但在Strings
对象中不会发生,因此您必须使用length()
来获取大小,并在必要时使用它进行迭代。 StringAPI7
StringAPI8
String
char by char you can do it with for
, while
and several more ways (splitting, converting...): 要按char循环String
char,可以使用for
, while
和其他几种方式(拆分,转换...)来实现: For loop example: 对于循环示例:
for (int i = 0; i < str.length(); i++)
While example: 而示例:
while (i < str.length()) {
Said that... Take a look at this code working and using what is explained: 这么说...看一下这段代码的工作原理, 并使用以下解释:
public static void main(String[] args) {
String str = "Hello";
int i = 0;
// string builder is a mutable string! :)
StringBuilder copy = new StringBuilder();
// we iterate from i=0 to length of the string (in this case 4)
while (i < str.length()) {
// same than copy = copy + str.charAt(i)
// but not creating each time a new String object
copy.append(str.charAt(i));
// goto next char
i++;
}
// print result
System.out.println(copy);
}
UPDATE 更新
thanks... but while I am trying this to find a reverse did not get result 谢谢...但是当我试图找到一个反向没有得到结果
If what you want is to reverse the String
(your code didn't do that, you must write copy = str.charAt(i) + copy;
) is much easier with StringBuilder
. 如果您想要反转String
(您的代码没有这样做,则必须编写copy = str.charAt(i) + copy;
),使用StringBuilder
会容易得多。 Take a look at this example: 看一下这个例子:
public static void main(String[] args) {
String str = "Hello";
StringBuilder copy = new StringBuilder(str);
copy.reverse();
System.out.println(copy);
}
FIrst way: 第一种方式:
Use the below code: 使用以下代码:
for (int i = 0; i < str.length(); i++) {
copy = copy+str.charAt(i);
}
Second Way: 第二种方式:
Convert String
into char[]
. 将String
转换为char[]
。 then convert it back to String
. 然后将其转换回String
。
char[] ch = str.toCharArray();
String copy = new String(ch);
System.out.println(copy);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.