Sunday, 15 June 2014

android - How can I effectively replace one or more characters -



android - How can I effectively replace one or more characters -

i have string separated commas follows

1,2,4,6,8,11,14,15,16,17,18

this string generated upon user input. suppose user wants remove of numbers, have rebuild string without specified number.

if current string is:

1,2,4,6,8,11,14,15,16,17,18

user intents remove 1, final string has be:

2,4,6,8,11,14,15,16,17,18

i tried accomplish using next code:

//string num number removed old = tv.gettext().tostring(); //old string newstring = old.replace(num+",",""); //will new string

this might working sure won't work above illustration have shown, if seek remove 1, removes lastly part of 11, because there exists 1.

well can utilize this. simplest approach can think of:

//string num number removed old=","+tv.gettext().tostring()+",";//old string commas added remove trailing entries newstring=old.replace(","+num+",",",");// new string newstring=newstring.substring(1,newstring.length()-1); // removing commas added

this work want do. have added comma @ start , end of string can remove first , lastly entries too.

android string replace

No comments:

Post a Comment