java str[newLength] = '\0' 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26558741/
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
What does str[newLength] = '\0' mean?
提问by Cindy
I have a question that what does str[newLength] = '\0' mean? because I think the last character should be str[newLength-1], so I don't know the meaning of this line.
我有一个问题,str[newLength] = '\0' 是什么意思?因为我觉得最后一个字符应该是str[newLength-1],所以不知道这行的意思。
Write a method to replace all spaces in a string with '%20'. Assume string has sufficient space at end of string to hold additional characters, and that you're given a true length of a string. I used the books code, implementing the solution in Java using a character array (given the fact that Java Strings are immutable):
编写一个方法,用“%20”替换字符串中的所有空格。假设字符串在字符串末尾有足够的空间来容纳额外的字符,并且您得到了字符串的真实长度。我使用了书籍代码,使用字符数组在 Java 中实现了解决方案(鉴于 Java 字符串是不可变的):
public class Test {
public void replaseSpaces(char[] str, int length) {
int spaceCount = 0, newLength = 0, i = 0;
for(i = 0; i < length; i++) {
if (str[i] == ' ')
spaceCount++;
}
newLength = length + (spaceCount * 2);
str[newLength] = 'public class StringURLify {
public static void URLify(String s) {
char[] c = s.toCharArray();
int length = s.length();
int spaceCount = 0, newLength = 0;
for(int i=0;i<= c.length-1;i++) {
if(c[i] == ' ') {
spaceCount++;
}
}
newLength = length + (spaceCount * 2);
char[] c1 = new char[newLength];
for(int i=length-1;i>=0;i--) {
if(c[i] == ' ') {
c1[newLength - 1] = '0';
c1[newLength - 2] = '2';
c1[newLength - 3] = '%';
newLength = newLength-3;
} else {
c1[newLength-1] = c[i];
newLength = newLength-1;
}
}
System.out.println("URLified string : " + String.valueOf(c1));
}
public static void main(String[] args) {
URLify("the dog left");
}
}
';
for(i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
str[newLength - 1] = '0';
str[newLength - 2] = '2';
str[newLength - 3] = '%';
newLength = newLength - 3;
}
else {
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
System.out.println(str);
}
回答by Damian Yerrick
The standard library of the C programming language commonly uses NUL-terminated strings. A NUL-terminated string is stored in an array, and the string itself consists of all the characters in an array prior to the first '\0'
character (called a NUL in ASCII). For example, if the elements of a character array are {'H','e','l','l','o','\0','i','d','1','0','t'}
, the string is "Hello"
, with everything after the NUL ignored. If you write '\0'
to position nof a NUL-terminated string, you will cut off everything after the first ncharacters, which reduces the string's length to no more than n. The writes to str[newLength - 1]
and the like write characters just before the NUL.
C 编程语言的标准库通常使用以NUL 结尾的字符串。以 NUL 结尾的字符串存储在数组中,字符串本身由数组中第一个'\0'
字符之前的所有字符组成(在 ASCII 中称为 NUL)。例如,如果字符数组的元素为{'H','e','l','l','o','\0','i','d','1','0','t'}
,则字符串为"Hello"
,忽略 NUL 之后的所有内容。如果您写入'\0'
以NUL 结尾的字符串的位置n,您将切断前n 个字符之后的所有内容,从而将字符串的长度减少到不超过n。write tostr[newLength - 1]
等在 NUL 之前写入字符。
I notice you're using the Java programming language, and you're probably trying to translate C code from your book literally to Java. Unlike C, Java doesn't usually use NUL-terminated strings, except in advanced things like JNI where Java code has to talk to C code. Also unlike C, you usually don't have to modify things in place but can instead freely create new copies, as Java has automatic garbage collection to get rid of objects and arrays that your code is no longer using.
我注意到您正在使用 Java 编程语言,并且您可能正在尝试将书中的 C 代码从字面上翻译成 Java。与 C 不同,Java 通常不使用以 NUL 结尾的字符串,除非在 JNI 等高级事物中 Java 代码必须与 C 代码对话。同样与 C 不同的是,您通常不必就地修改内容,而是可以自由地创建新副本,因为 Java 具有自动垃圾收集功能,可以清除代码不再使用的对象和数组。
Java more often uses the StringBuilder
classto construct a string before making an immutable String
out of it. So an idiomatic Java solution would create an empty StringBuilder
, loop through the charactersin an existing String
, append("%20")
for each character that is a space or append
the same character otherwise, and finally convert the StringBuilder
instance to a String
.
Java 更经常地使用StringBuilder
该类来构造一个字符串,然后再将它变成不可变String
的。因此,惯用的 Java 解决方案将创建一个空的StringBuilder
,循环遍历现有 中的字符String
,append("%20")
对于每个空格或append
相同字符的字符,最后将StringBuilder
实例转换为String
.
回答by wittakarn
Java is not C nor C++; Java doesn't use the '\0' character to indicate the end of string ('end of char array' actually in C and C++). Java keeps a 'length' field for arrays so it can do bound checking behind the scenes.
Java 既不是 C 也不是 C++;Java 不使用 '\0' 字符来指示字符串的结束(实际上在 C 和 C++ 中为'字符数组结束')。Java 为数组保留了一个“长度”字段,因此它可以在幕后进行边界检查。