2

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

3
  • 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. Commented Jul 23, 2013 at 16:34
  • As I said I want to only manipulate the array , not using any additional data structure (i.e. in-place) Commented Jul 23, 2013 at 16:37
  • 2
    String builder is a class that uses an array of chars for string manipulation. Commented Jul 23, 2013 at 16:41

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.