java - How to process a line that has 2 same words -
i have line follows: abcde, def, efgh, mnop, mno, pqr, abc, abcde, abcde, mnop, efg
in this, abcde , mnop occur more once. want change names of occurrences of abcde , mnop represented different.
how can without changing order of sequence?
please note words appear more once (more twice in cases) not known. need figure out words appears more once done.
the line string , want end result string processing.
thanks in advance.
public class stringmod { public static void main(string[] args) { string text = "abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg, abcde"; string[] sp = text.split(", "); int count = 1; for(int i=0;i<sp.length;i++){ count = 1; for(int j=i+1;j<sp.length;j++){ if(sp[i].equals(sp[j])){ count++; sp[j]=sp[j]+" "+count; } } } string returnstring = ""; for(int i=0;i<sp.length-1;i++) returnstring+=sp[i]+", "; returnstring+=sp[sp.length-1]; system.out.println(returnstring); } }
something should work ....
input :
abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg, abcde
output :
abcde, def, efgh, mnop, mno, pqr, abc, abcde 2, mnop 2, efg, abcde 3
hope helps.
Comments
Post a Comment