Skip to content
Advertisement

Change all letters except space from a string using Java

I want to change all letters from a string to “-” char except space using Java.

I tried:

String out = secretWord.replaceAll("^ " , "-");

and

String out = secretWord.replaceAll("\s" , "-");

They didn’t work.

I tried:

String newWord = secretWord.replaceAll("[A-Z]" , "-");

It worked but i didn’t change Turkish characters I use in that string.

Original Code:

public class ChangeToLine {

    public static void main(String[] args) {
    
        String originalWord = "ABİDİKUŞ GUBİDİKUŞ";
        String secretWord = originalWord;
    }
}

Advertisement

Answer

You can use the \S regex:

String s = "Sonra görüşürüz";
String replaced = s.replaceAll("\S", "-");
System.out.println(replaced); // outputs ----- ---------
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement