remove special character [ ] in array java -
in program have following string of array, when process program output have square bracket [], need without square bracket []. suggestion in how remove them?
private final static string[] l0 = {"az","fh md br", "inr gt cn", "bl gs st st", "mae nw get", "pam ml rm", "comr lab pl mt hs", "za"}; public static string sfuffle() { list<string> shuffled = new arraylist<string>(arrays.aslist(phrasestring)); collections.shuffle( shuffled ); system.out.println(shuffled);// added have output return shuffled + "\n"; } output:
[az, mae nw get, bl gs st st, fh md br, za, comr lab pl mt hs, inr gt cn, pam ml rm] my desired output be:
az, mae nw get, bl gs st st, fh md br, za, comr lab pl mt hs, inr gt cn, pam ml rm
just use substring():
string str = shuffled.tostring(); return str.substring(1, str.length() - 1) + "\n"; by popular demand, i'll add explanation of why you're getting string brackets in first place. when write like
shuffled + "\n" this converted to
new stringbuilder().append(shuffled).append("\n") stringbuilder class designed string concatenation , manipulation. when append object (shuffled, in case), string returned object's tostring() method appended. now, shuffled arraylist, , uses tostring() method defined in abstractcollection. can see documentation tostring() return string of form [e1, e2, ..., en] (where each ei element of collection). of course, "\n" newline , not directly visible.
Comments
Post a Comment