Is it possible to replace multiple characters in a string with one? I saw some ways to do this with loops, but isnt it possible to do it easier?
For example:
input: /////Hello//this////////is///Java
output: /Hello/this/is/Java
Something like this:
String s = "/////Hello//this////////is///Java";
System.out.println(s.replaceAll("/+", "/"));
"/////Hello//this////////is///Java".replaceAll("/+", java.io.File.separator) works fine. However this separator is taken from FileSystem implementation that may be different on your system. What character it gives you? And what did you try?java.io.File.separator.replace("\\", "\\\\") to be used as replacement.This is a complete answer:
String Str = new String("Your string");
Set<Character> charsOfString = new HashSet<Character>();
int len = Str.length();
for(int i = 0 ; i < len ; i++)
charsOfString.add(Str.charAt(i));
for (Character c : charsOfString)
Str = Str.replaceAll(c + "+", c + "");
This will delete all duplicated characters of your string