简体   繁体   English

C ++和Java中的字符串之间的区别

[英]difference between Strings in C++ and Java

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. 但是在Java ,如果我尝试做同样的事情,它会抛出一个错误。 Why?. 为什么?。

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? C++的字符串实现与C++不同?

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. 基本上,C ++允许用户定义的运算符 - Java不允许。 So the String class doesn't expose any sort of "indexing" operator; 所以String类不公开任何类型的“索引”运算符; that only exists for arrays, and a String isn't an array. 存在于数组中,而String不是数组。 (It's usually implemented using an array, but that's a different matter.) (它通常使用数组实现 ,但这是另一回事。)

EDIT: As noted in comments, the + operator is special-cased for strings - right in the language specification. 编辑:如注释中所述, +运算符特殊的字符串 - 在语言规范中。 The same could have 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. 对于[] 也可以这样做,但它不是 - 并且因为它不在语言规范中,并且Java不支持重载运算符,所以它不能在库代码中执行。 (For example, you can't give custom behaviour to + for any other class.) (例如,你不能给定制行为+任何其他类)。

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. 它们都以不能更改它们的方式存储字符串字符。

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. 另一方面,Java不允许任何类型的运算符重载(唯一的例外是为字符串重载的+运算符),因此,索引运算符不能也不能为String对象重载。 In Java, to access a character of a string, you need to use accessor member methods, such as charAt . 在Java中,要访问字符串的字符,您需要使用访问者成员方法,例如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: 您还可以调用String类的toCharArray方法,该方法返回字符串对象的字符数组,您可以使用索引运算符和返回值:

char c = s.toCharArray()[i];

See the method String#charAt 请参阅String#charAt方法

Returns the char value at the specified index. 返回指定索引处的char值。 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. 索引的范围从0到length() - 1.序列的第一个char值在索引0处,下一个在索引1处,依此类推,就像数组索引一样。

If the char value specified by the index is a surrogate, the surrogate value is returned. 如果索引指定的char值是代理项,则返回代理项值。

public char charAt(int index)

in c++ strings are already treated as array of characters, but in java String is a built in class. 在c ++字符串中已经被视为字符数组,但在java中,String是一个内置类。 it is different from array of characters. 它与字符数组不同。

In C++, a string is typically just an array of (or a pointer to) chars, terminated with a NULL (\\0) character. 在C ++中,字符串通常只是一个字符数组(或指向字符的指针),以NULL(\\ 0)字符结尾。 You can process a string by indexing also as you would process any array. 您也可以像处理任何数组一样通过索引处理字符串。
But in Java , a strings are not arrays. 但在Java中,字符串不是数组。 Java strings are objects of type java.lang.String so You cannot process them by indexing . Java字符串是java.lang.String类型的对象,因此您无法通过索引处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM