I want to change all letters from a string to “-” char except space using Java.
I tried:
JavaScript
x
String out = secretWord.replaceAll("^ " , "-");
and
JavaScript
String out = secretWord.replaceAll("\s" , "-");
They didn’t work.
I tried:
JavaScript
String newWord = secretWord.replaceAll("[A-Z]" , "-");
It worked but i didn’t change Turkish characters I use in that string.
Original Code:
JavaScript
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:
JavaScript
String s = "Sonra görüşürüz";
String replaced = s.replaceAll("\S", "-");
System.out.println(replaced); // outputs ----- ---------