End of string character in Java

29,168

Solution 1

Java doesn't "mark" the end-of-string as C does. It tracks length & values, so it's possible to have zero-chars (\0) in the string. If you create a String from a char array containing \0 chars, the resultant String will contain those characters.

Note also, an array has a static size - it cannot be re-sized, so you'll have to track the "size" yourself (the String constructor won't drop undesirable characters for you from the end of a char array).

Consider using either:

  • A StringBuilder as suggested by Clyde Byrd III, or

  • String(char value[], int offset, int count); you'll have to determine programmatically what the appropriate values for offset and count would be. Perhaps String methods, such as replace, concat, or substring might help.

Solution 2

Firstly, a String is not a char[]. A String uses a char[], but that's all.

What you're suggesting is a custom interpretation of a special character to mark the "used range" of an array of char, which is fine if that's the contract you are creating for your method.

All characters are "valid" characters in java, but there are unicode "non characters" that are officially guaranteed to never be used for encoding a character: \uFDD0 through \uFDEF, \uFFFE and \uFFFF.

You may consider using one of these as your "end of range" marker character.

Share:
29,168
Arian
Author by

Arian

I build scalable and resilient software systems.

Updated on July 24, 2020

Comments

  • Arian
    Arian almost 4 years

    I was solving an easy question :

    Remove certain characters of a character array in Java , the idea is straightforward :

    static void remove_char(char[] arr, char c){
        int r = 0;
        for (int i = 0 ; i < arr.length ; i++){
            if (arr[i] == c){
                r++;
                continue;
            }
            arr[i - r] = arr[i];
        }
        arr[arr.length - r] = '\0'; // ??
        return;
    }
    

    I want to put an ending character which signals that the rest of the array doesn't have to be considered when we want to, for example, generate a string using new String(arr)

    Is there any such character in Java ? ( I guess in C it's \0 but I am not sure)

    For example when we call :

    System.out.println(new String(remove_char(new char[] {'s','a','l','a','m'} , 'a')))

    this will be printed : slm m

    While I want to get slm and I want to do this in-place not using a new array