java - How can I move the first word to the end? -
enter line of text. no punctuation please.
java language
have rephrased line read:
is language java
this example, , know char method, don't know how move first word end. string method can use?
what do:
- split sentence using string.split();
- create list items
- reorder list
- join list items using space
implementation in plain java:
final string s = "java language"; final list<string> list = new arraylist<string>(arrays.aslist(s.split("\\s+"))); list.add(list.size() - 1, list.remove(0)); final stringbuilder sb = new stringbuilder(); for(final string word : list){ if(sb.length() > 0){ sb.append(' '); } sb.append(word); } system.out.println(sb.tostring());
implementation using guava:
final string s = "java language"; final list<string> list = lists.newarraylist(splitter .on(charmatcher.whitespace) .omitemptystrings() .split(s)); list.add(list.size() - 1, list.remove(0)); system.out.println(joiner.on(' ').join(list));
Comments
Post a Comment