java - Add something after an argument? -


okay writing simple little stringbuilder, curious how add after arguments. example if do:

args[0] = "how" args[1] = "do" args[2] = "i" args[3] = "google" 

how make output this: "how+do+i+google", without plus @ end of last argument?

this have far.

stringbuilder sb = new stringbuilder(); (int = 0; < args.length; i++){     sb.append(args[i]).append(" "); }  string allargs = sb.tostring().trim(); 

with pre-jdk8 can use guava's joiner:

return joiner.on("+").skipnulls().join(args); 

for jdk8, @chengpohi seems enough, little correction:

return string.join("+", args); 

to extend op code:

stringbuilder sb = new stringbuilder(); (int = 0; < args.length; i++){     sb.append(args[i]);     if(i != (args.length - 1)) {         sb.append('+');     } }  string allargs = sb.tostring().trim(); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -