java string output in reverse order of words

String str = "hi,hello world !";
 
// separator
char[] splits = ", ".toCharArray();
Set<Character> splitCharSet = new HashSet<Character>();
for(char split:splits){
    splitCharSet.add(split);
}


StringBuilder reverseStr = new StringBuilder();
StringBuilder rsIndex = new StringBuilder();
Character indexChar;
for(int i=str.length()-1;i>-1;i--){
    indexChar = Character.valueOf(str.charAt(i));
    if(!splitCharSet.contains(indexChar)){
        rsIndex.append(indexChar);
    }else{
        reverseStr.append(rsIndex.reverse()).append(indexChar);
        rsIndex.delete(0, rsIndex.length());
    }
}
reverseStr.append(rsIndex.reverse());
System.out.println(reverseStr);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326648194&siteId=291194637