Zuzuzu-kun
Zuzuzu-kun

Reputation: 91

Removal of comma in the end of a string android

i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code

public void onClick(View v) {
            String space = "";
            String foo = " ";
            String foo1 = ",";
            String sentences = null;

            //Splitting the sentence into words
            sentences = multiple.getText().toString().toLowerCase();
            String[] splitwords = sentences.trim().split("\\s+");

            for (String biyak : splitwords) {
                foo = (foo + "'" + biyak + "'" + foo1);


                foo.replaceAll(",$", " ");//foo.replaceAll();
                wordtv.setText(foo);

My codes Output : 'Hello','world', My desire output: 'Hello','world'

Upvotes: 0

Views: 114

Answers (3)

Ben P.
Ben P.

Reputation: 54204

String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).

Upvotes: 1

vishal prajapati
vishal prajapati

Reputation: 3

U can also use a if statement and traverse the whole string . If a comma(,) is found replace it with a space(" ").

Upvotes: 0

Bipin Gawand
Bipin Gawand

Reputation: 561

you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer class
StringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);

Upvotes: 0

Related Questions