简体   繁体   English

str[newLength] = '\\0' 是什么意思?

[英]What does str[newLength] = '\0' mean?

I have a question that what does str[newLength] = '\\0' mean?我有一个问题,str[newLength] = '\\0' 是什么意思? because I think the last character should be str[newLength-1], so I don't know the meaning of this line.因为我觉得最后一个字符应该是str[newLength-1],所以不知道这行是什么意思。

Write a method to replace all spaces in a string with '%20'.编写一个方法,用“%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):我使用了书籍代码,使用字符数组在 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] = '\0';
    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);
}

The standard library of the C programming language commonly uses NUL-terminated strings . C 编程语言的标准库通常使用以NUL 结尾的字符串 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).以 NUL 结尾的字符串存储在数组中,字符串本身由数组中第一个'\\0'字符(在 ASCII 中称为 NUL)之前的所有字符组成。 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.例如,如果字符数组的元素为{'H','e','l','l','o','\\0','i','d','1','0','t'} ,字符串是"Hello" ,忽略 NUL 之后的所有内容。 If you write '\\0' to position n of a NUL-terminated string, you will cut off everything after the first n characters, which reduces the string's length to no more than n .如果您将'\\0'写入以 NUL 结尾的字符串的位置n ,您将切断前n 个字符之后的所有内容,从而将字符串的长度减少到不超过n The writes to str[newLength - 1] and the like write characters just before the NUL.写入str[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.我注意到您正在使用 Java 编程语言,并且您可能正在尝试将书中的 C 代码从字面上翻译成 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.与 C 不同,Java 通常不使用以 NUL 结尾的字符串,除非在 JNI 等高级事物中,Java 代码必须与 C 代码对话。 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.同样与 C 不同的是,您通常不必就地修改内容,而是可以自由地创建新副本,因为 Java 具有自动垃圾收集功能,可以清除代码不再使用的对象和数组。

Java more often uses the StringBuilder class to construct a string before making an immutable String out of it. Java 更经常地使用StringBuilder来构造一个字符串,然后再从中生成一个不可变的String So an idiomatic Java solution would create an empty StringBuilder , loop through the characters in 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字符,为每个空格字符append("%20") ,否则append相同的字符,最后将StringBuilder实例转换为String .

Java is not C nor C++; Java 既不是 C 也不是 C++; Java doesn't use the '\\0' character to indicate the end of string ('end of char array' actually in C and C++). Java 不使用 '\\0' 字符来指示字符串的结束(实际上在 C 和 C++ 中为'字符数组结束')。 Java keeps a 'length' field for arrays so it can do bound checking behind the scenes. Java 为数组保留了一个“长度”字段,因此它可以在幕后进行边界检查。

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");
    }
}

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

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