Home > Software design >  How to replace text surrounded with stars with bolded text in my message
How to replace text surrounded with stars with bolded text in my message

Time:10-10

I am making a chat app and I want to add a feature which if the user types text between two stars (*) , the text between the two stars bold.

For example if the user types the messages as *this is bold* and this is not,the message between stars is bold and the message outside if not bold.

I am unable to get the logic to do it. The code I have currently written is :

    String[] boldChecker = chatMessage.message.split("\\*");
                for (int i = 0; boldChecker.length > i ; i  ){
                    if ((i % 2) == 0) {
                        //is even and not bold
                        Log.d("boldArea - No",boldChecker[i]);
                    } else {
                        //is odd and bold
                        Log.d("boldArea - Yes",boldChecker[i]);
                    }
                }

Here chatmessage.message is the message typed by the user. The only thing I know is that to set the text to bold, I need to use html.fromHtml(<b>bold text</b>).

CodePudding user response:

You can proceed with this approach:

Simply replace pairs of * with <b> </b> tags and then use Html.fromHtml(message) to make the message bold wherever user typed the message between pair of *

Like this:
let message = "*This is bold* and this is not *"
then change message = "<b>This is bold</b> and this is not *"

You can use this code for replacement


        // Let the message typed by the user is this
        String msg = "Hello friends, *This is the bold text* and this is not but *this is also bold* ** *";

        /**
         * @params firstIndex = index of the first * of the pair we are looking in the msg string
         *         secondIndex = index of the second * of the pair
         *         pairCount = to help us acknowledge that the pair *, we are looking for, is ready
         */
        int firstIndex = 0, secondIndex = 0, pairCount = 0;

        int i = 0;
        while (true){

            char c = msg.charAt(i);

            if (c == '*'){
                if (pairCount == 0) {
                    pairCount  = 1;
                    firstIndex = i;
                }
                else if (pairCount == 1) {
                    pairCount  = 1;
                    secondIndex = i;
                }
            }

            if (pairCount == 2){

                // replacing first * with <b>
                msg = msg.substring(0,firstIndex)   "<b>"   msg.substring(firstIndex 1);


                // replacing second * with </b>
                // increasing secondIndex by 2
                // because length of "msg" is increased by 2
                // after replacing <b> with *
                secondIndex  = 2;
                msg = msg.substring(0,secondIndex)   "</b>"   msg.substring(secondIndex 1);


                // changing pair count to 0 as we have replaced the * pair
                // and are looking for new pair
                pairCount = 0;


                // Increasing i value to 5 because we have added <b> and </b> (total length = 7)
                // on replacement of * pairs (total length = 2)
                // 7 - 2 = 5
                i  = 5;

            }

            i  = 1;

            if (i >= msg.length()) break;
        }


        // Set the text to the TextView using Html 
        TextView tv = findViewById(R.id.your_textview_id);
        tv.setText(Html.fromHtml(msg));

Result bold text example

Hope this helps

  • Related