Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android splitting with space not working for this case. Why?

I need to highlight and make url in the text clickable, dynamically.

For that, I am using the below method

private SpannableString addClickablePart(String string) {


        string = string.replaceAll("\\n"," \n ");

        string += " ";
        SpannableString ss = new SpannableString(string);
        String[] words = string.split(" ");
        for (final String word : words) {


            if (CommonUtilities.isValidURL(word)) {


                int lastIndex = 0;

                while(lastIndex != -1){

                    lastIndex = string.indexOf(word+" ",lastIndex);

                    if(lastIndex != -1){
                        ClickableSpan clickableSpan = new ClickableSpan() {
                            @Override
                            public void onClick(View textView) {
                                //use word here to make a decision

                                isRefreshingNecessary = false;

                                Intent mIntent = new Intent(ctx, UserWebsiteActivity.class);
                                mIntent.putExtra("website", word);
                                startActivity(mIntent);
                            }
                        };

                        ss.setSpan(clickableSpan, lastIndex, lastIndex + word.length(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                        lastIndex += word.length();
                    }
                }


            }
        }

        return ss;
    }

Its working for most of the cases. But, not working for all the cases as the below example.

The pricing information provided to you in your plan terms and conditions about these number ranges will no longer apply and will be replaced by this charging structure. See www.ee.co.uk/ukcalling for further information.

As, for the above case, when I split the whole string using

 String[] words = string.split(" ");

or

 String[] words = string.split("\\s+");

I got See www.ee.co.uk/ukcalling for as a single word. Instead, I need these 3 - See,www.ee.co.uk/ukcalling and for as 3 different words, not to be as grouped as a single word.

I am unable to understand whats wrong in the way of splitting with space. Please help me to know.

like image 263
Narendra Singh Avatar asked Feb 25 '16 11:02

Narendra Singh


People also ask

How do you split a string with spaces?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you separate text on Android?

split() is documented with TextUtils. split(): String. split() returns [''] when the string to be split is empty.

How do you split a space in Java?

You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.

How do you split a string with first space in Kotlin?

We can use the split() function to split a char sequence around matches of a regular expression. To split on whitespace characters, we can use the regex '\s' that denotes a whitespace character. Note that we have called the toTypedArray() function, since the split() function returns a list.


1 Answers

Replace all non visible white space characters.

 string = string.replaceAll("\\t", " ");
 string = string.replaceAll("\\xA0", " ");
 string = string.replaceAll("\\u1680", " ");
 string = string.replaceAll("\\u180e", " ");
 string = string.replaceAll("\\u2000", " ");
 string = string.replaceAll("\\u200a", " ");
 string = string.replaceAll("\\u202f", " ");
 string = string.replaceAll("\\u205f", " ");
 string = string.replaceAll("\\u3000", " ");

for java 8

string = string.replaceAll("(^\\h*)|(\\h*$)","");

check this how-to-trim-no-break-space-in-java

like image 172
sgpalit Avatar answered Oct 18 '22 03:10

sgpalit