Home > front end >  In java, is there a way to convert a control character to a set of characters in a string?
In java, is there a way to convert a control character to a set of characters in a string?

Time:12-28

Sorry if the title was poorly phrased, but I am trying to make a program that can find a way of detecting whether each individual character in a string is a control character or not. If there is a control character, it should replace that control character with a set of characters. For example, the String "an/0Example" should become "anotherExample", assuming that "other" is what I want to replace with the control character. The code I have so far is below, but my understanding is that a character, with the exception of a control character, cannot contain more than one character.

    int i = 0;
    char exampleCh = identifier.charAt(i);
    
    for(i = 0; i < identifier.length(); i  ){
        if(Character.isISOControl(example)){
            identifier = identifier.replace(exampleCh, 'exampleReplacement');
        }
     }

CodePudding user response:

Assuming we have to following input string:

String str = "an\0Example";

We can replace the control character using conventional loop and a string builder:

String replacement = "another";
StringBuilder stringBuilder = new StringBuilder();
for (char c : str.toCharArray()) {
    stringBuilder.append(Character.isISOControl(c) ? replacement : c);
}
String result = stringBuilder.toString();

Or we can use streams:

String result = str.chars()
        .mapToObj(c -> Character.isISOControl(c) ? replacement : String.valueOf((char)c))
        .collect(Collectors.joining());

Please note, according to isISOControl method, this an/0Example does not contain any kind of control character. Instead of /0, I believe you would wanted to have this \0.

CodePudding user response:

Escapes processed by compiler

The escapes embedded in a string literal are processed by the compiler, and transformed as the intended character. So at runtime, the string object contains a null, and does not contain a backslash and zero seen in your literal "an\0Example".

You can see this in the following code.

    String input = "an\0Example" ;
    System.out.println(
        Arrays.toString(
            input.codePoints().toArray()
        )
    );

See this code run live at IdeOne.com. Notice the zero in third position, a null character, followed by the seven characters of the word “Example”.

[97, 110, 0, 69, 120, 97, 109, 112, 108, 101]

Avoid char

Never use char type. That type has been legacy as of Java 2, essentially broken. As a 16-bit value, char is physically incapable of representing most characters.

Code points

Use code point integer numbers instead.

StringBuilder sb = new StringBuilder() ;
for( int codePoint : input.codePoints().toArray() ){
    if( ! Character.isISOControl( codePoint ) ) {
        sb.appendCodePoint( codePoint ) ;
    }
 }
String output = sb.toString() ;

Dump to console.

System.out.println( output ) ;
System.out.println( Arrays.toString( output.codePoints().toArray() ) ) ;
}
System.out.println( output.codePoints().mapToObj( Character :: toString ).toList() ) ;

See this code run live at IdeOne.com.

anExample

[97, 110, 69, 120, 97, 109, 112, 108, 101]

[a, n, E, x, a, m, p, l, e]

  • Related