简体   繁体   中英

How to get some specific lines in a textview that have a particular symbol

I have a textview which has a long text lines like below:

$hello all my name is Ford
I'm an electronic engineer and so blah blah blah
and something else here
so other texts....
$and this is another line like the first line
and so other normal texts

my question is how can I get just the lines that have the $ symbol on the first of those and set some effects to just those specific lines like Bold and red color.. not other lines...

Any Idea??!
Thanks for your helps!

Edit:
note that my textview texts are dynamic and I cant write code for some specific sentences. I want to write some code as template to every line that for example starts with $ , be bold or something like that.
and also I want to the $ symbole itself; doesn't show in my text view after this proccess! any idea for this?

If you already have the text on String you could use split function on String value and then display your line with html format that make you able to manipulate the size and format here is a sample:

        String stringValue="$your first line\n $your second line";
        String[] Splited=stringValue.split("$");
        //first line is in the Splited[0] and second line is in the Splited[1]
        textview.setText(Html.fromHtml("<h2>Splited[0]</h2><br><p>Spl_j[Z B1]</p>"));;

UPDATE: your template could be like below:

 String[] Splited=stringValue.split("\n");
 String msg="";           
    for(int i=0;i<Splited.length;i++){
       if(Splited[i].startsWith("$"))
          msg=msg+"<b>"+Splited[i]+"</b>";
       else
          msg=msg+Splited[i];

}
textview.setText(Html.fromHtml(msg));

you can also do it like this, using search and replace

String yourValue="$your first line\n $your second line";
String regex = "(\$.*)\n";
String formattedValue = yourValue.replaceAll(regex, "<b>$1</b>\n");
textview.setText(Html.fromHtml(formattedValue));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM