End of string character in Java

原文转自:http://stackoverflow.com/questions/17815755/end-of-string-character-in-java

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

End of string character in Java

up vote 0 down vote favorite

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

share improve this question
 
2  
Using the StringBuilder class out of the question? docs.oracle.com/javase/tutorial/java/data/buffers.html. It probably doesn't help you figure out a solution to the above though. It does supply a solution. –   CBIII  Jul 23 '13 at 16:34 
 
As I said I want to only manipulate the array , not using any additional data structure (i.e. in-place) –  Arian Hosseinzadeh  Jul 23 '13 at 16:37
2  
String builder is a class that uses an array of chars for string manipulation. –   CBIII  Jul 23 '13 at 16:41 

2 Answers

active oldest votes
up vote 6 down vote accepted

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:

  • 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 replaceconcat, or substring might help.

share improve this answer
 
up vote 2 down vote

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 improve this answer
 

Your Answer


你可能感兴趣的:(java,String,end,of)