Kushal Shah
Kushal Shah

Reputation: 1333

Trim or replace all commas and spaces from the String

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.

Upvotes: 4

Views: 32572

Answers (7)

Prasad Bhosale
Prasad Bhosale

Reputation: 722

yourString=yourString.replaceAll("^\\W+|\\W+$","");

Upvotes: 2

Julian B.
Julian B.

Reputation: 3725

After trying to accomplish this myself, this seemed to work beautifully. Say you are attempting to do this for a string called myString:

    // replace all consecutive spaces and commas by a space and a comma respectively
    String sanitizedString = myString.replaceAll("\\s+", " ").replaceAll(",+", ",");

    // replace all resulting comma and space combinations
    sanitizedString = sanitizedString.replaceAll("( , )+|( ,)+|(, )+", ",").replaceAll(",+", ",");

    // remove leading and trailing commas (if any)
    sanitizedString = sanitizedString.replaceFirst("^,", "").replaceFirst(",$", "");

    // add a space between comma (merely for readability) **** optional ****
    sanitizedString = sanitizedString.replace(",", ", ");

Upvotes: 3

RanRag
RanRag

Reputation: 49567

You can try this its not a proper soution but will work in your case

String str = "kushal,mayurv,narendra,dhrumil,mark, ,,,, ,";

String new_str = str.replaceAll(","," "); //replace , with space

String temp = new_str.trim();             //remove trailing space

System.out.println(temp.replaceAll(" ",",")); // now replacing space with ,

Upvotes: 2

João Portela
João Portela

Reputation: 6456

Since it seems that you are trying to remove commas and spaces at the end of the string this might be what you are looking for, this pattern is usefull [, ]+$

Pattern p = Pattern.compile("[, ]+$");
Matcher m = p.matcher("kushal,mayurv,narendra,dhrumil,mark, ,,,, ,");
String clean = m.replaceFirst("");

you can see this sample running here

edit
if you want an array with the individual entries string.split is the way to go (see user949300 answer)

Upvotes: 1

user949300
user949300

Reputation: 15729

In general, a regex replaceAll() is the way to go. However, in your case, do you eventually want an array of the individual Strings? e.g. the equivalent of

new String[] { "kushal", "mayurv", "narendra", "dhrumil", "mark" }  ?

If so, you can do

String[] split = yourString.split(",");

but then go through split very carefully and look for empty Strings.

...

I see that Sunil beat me to it - check out that answer.

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

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);

Upvotes: 5

auo
auo

Reputation: 572

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.

Upvotes: 5

Related Questions