Hi i am developing android application.Now i stuck in one problem. Let me give you a example to understand my problem.
What I have is : kushal,mayurv,narendra,dhrumil,mark, ,,,, ,
What i want is : kushal,mayurv,narendra,dhrumil,mark
Any help is appreciated.
Try with the following code to trim all unwanted comma and whitespaces
String str = "kushal,mayurv,narendra,dhrumil,mark, ,,,, ";
String splitted[] = str.split(",");
StringBuffer sb = new StringBuffer();
String retrieveData = "";
for(int i =0; i<splitted.length; i++){
retrieveData = splitted[i];
if((retrieveData.trim()).length()>0){
if(i!=0){
sb.append(",");
}
sb.append(retrieveData);
}
}
str = sb.toString();
System.out.println(str);
Use regex to solve it. You want to remove all (,) that are followed by space or another (,). You also want to remove all (,) that isn't followed by a letter.
Regex in Android
yourstring = yourstring.replaceAll("( ,)|(,,)", "");
Something like that, sorry that I can't help you more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With