C++ 和 Java 中字符串的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18888152/
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
difference between Strings in C++ and Java
提问by TheLostMind
In C++
I can do something like this...
在C++
我可以做这样的事情......
String s = "abc";
char c = s[i]; // works fine...
But in Java
, if I try doing the same, it throws an error. Why?.
但是在 中Java
,如果我尝试这样做,则会引发错误。为什么?。
In java
, to achieve the above, I have to do :
在java
,要实现上述目标,我必须执行以下操作:
s.toCharArray();
How is the implementation of Strings in C++
different from that in Java?
Strings 的实现与C++
Java 中的实现有何不同?
采纳答案by Jon Skeet
In java, to achieve the above, I have to do : s.toCharArray();
在java中,要实现上述目的,我必须这样做:s.toCharArray();
Not really. You can use charAt
instead:
并不真地。您可以charAt
改用:
char c = s.charAt(i);
Basically, C++ allows user-defined operators - Java doesn't. So the String
class doesn't expose any sort of "indexing" operator; that onlyexists for arrays, and a String
isn't an array. (It's usually implementedusing an array, but that's a different matter.)
基本上,C++ 允许用户定义的运算符 - Java 不允许。因此,String
该类不会公开任何类型的“索引”运算符;即只存在阵列和一个String
不是数组。(它通常使用数组实现,但那是另一回事。)
EDIT: As noted in comments, the +
operator isspecial-cased for strings - right in the language specification. The same couldhave been done for []
, but it isn't - and as it's not in the language specification, and Java doesn't support overloaded operators, it can't be performed in library code. (For example, you can't give custom behaviour to +
for any other class.)
编辑:如评论中所述,+
运算符是字符串的特殊情况 - 就在语言规范中。也可以对 做同样的事情[]
,但事实并非如此——而且由于它不在语言规范中,而且 Java 不支持重载运算符,因此不能在库代码中执行。(例如,您不能+
为任何其他类提供自定义行为。)
回答by Suresh Atta
See the method String#charAt
参见方法String#charAt
Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
If the char value specified by the index is a surrogate, the surrogate value is returned.
返回指定索引处的 char 值。索引范围从 0 到 length() - 1。序列的第一个字符值在索引 0 处,下一个在索引 1 处,依此类推,对于数组索引。
如果索引指定的 char 值是代理项,则返回代理项值。
public char charAt(int index)
回答by andy256
The difference is that C++ has operator overloading, and uses it to access the string contents.
不同之处在于 C++ 具有运算符重载,并使用它来访问字符串内容。
They both store the string characters in such a way as you cannot change them.
它们都以无法更改的方式存储字符串字符。
回答by Prinz Km
in c++ strings are already treated as array of characters, but in java String is a built in class. it is different from array of characters.
在 c++ 中字符串已经被视为字符数组,但在 java 中字符串是一个内置类。它不同于字符数组。
回答by Prabhaker A
In C++, a string is typically just an array of (or a pointer to) chars, terminated with a NULL (\0) character. You can process a string by indexing also as you would process any array.
But in Java , a strings are not arrays. Java strings are objects of type java.lang.String
so You cannot process them by indexing .
在 C++ 中,字符串通常只是一个(或指向)字符的数组,以 NULL (\0) 字符结尾。您也可以通过索引处理字符串,就像处理任何数组一样。
但是在 Java 中,字符串不是数组。Java 字符串是类型对象,java.lang.String
因此您不能通过索引来处理它们。
回答by MxNx
The reason that it is possible to write
可以写的原因
string s = "abc";
char c = s[i];
in C++ is that the string
class has overloaded the indexing operator (say [] operator) which allows programmers to access characters of a string
object the same way that they access an element of an array, despite the fact that a string
object is not an array.
在 C++ 中,string
类重载了索引运算符(比如 [] 运算符),它允许程序员以与访问string
数组元素相同的方式访问对象的字符,尽管string
对象不是数组。
Java, on the other hand, does not allow operator overloading of any kind (the only exception is the +
operator that is overloaded for strings) and hence, the indexing operator is not and can not be overloaded for String
objects. In Java, to access a character of a string, you need to use accessor member methods, such as charAt
. You can also invoke the toCharArray
method of the String
class, which returns to you an array of the characters of the string object and you can use the indexing operator with this returned value:
另一方面,Java 不允许任何类型的运算符重载(唯一的例外是+
为字符串重载的运算符),因此,索引运算符不能也不能为String
对象重载。在 Java 中,要访问字符串的字符,需要使用访问器成员方法,例如charAt
. 您还可以调用该类的toCharArray
方法,该方法String
会返回一个字符串对象的字符数组,您可以使用带有此返回值的索引运算符:
char c = s.toCharArray()[i];