Hello, there is a text in a variable, before displaying this text in a textview, you need to select bold text sections from ( bracket to) the bracket , and from the beginning of the text to the sign - ( dash ).

I tried this method, but I can select only a certain number of words, in this case two, and I need something specifically from the beginning of the text to - (dash). Because, in a variable, the text changes, and up to a dash there can be not two words, and not three, but more or less.

String[] tokens = e.split(" "); String twoFirstWords = tokens[0] + " " + tokens[1]; //первые два слова String content1 = e.replaceFirst(twoFirstWords, " "); //остальной текст SpannableStringBuilder builder = new SpannableStringBuilder(); SpannableString span1 = new SpannableString(twoFirstWords); SpannableString span2 = new SpannableString(content1); span1.setSpan(new StyleSpan(Typeface.BOLD), 0, twoFirstWords.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append(span1); builder.append(span2); f2.setText(builder); 
  • Give an example of the text - V. Makhnutin
  • @ V.Makhnutin Alexander the Great of Macedon (Alexander III the Great) - the Macedonian king from (336 BC) from the Argead dynasty, commander, creator of a world power that fell apart after his death. In the Muslim tradition can be identified with the legendary king Zul-Karnayn. In Western historiography is best known as Alexander the Great. Even in Antiquity, Alexander gained the fame of one of the greatest commanders in history - Peter.

1 answer 1

Just make a split on the dash and take the zero element, this will be what is before the first in the text dash.

 String[] tokens = e.split("—") String beforeDash = tokens[0]; 

The text between the brackets can be searched as follows:

 Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(value); while(m.find()) { doSomethingWithString(m.group(1)); } 
  • Thanks helped, and how to select and make bold the text from the bracket to the bracket? - Peter
  • those. from the bracket to the bracket after the dash? in all brackets? - V. Makhnutin
  • In all brackets - Peter
  • Completed the answer by searching for expressions between brackets. - V. Makhnutin
  • Please tell me, how can I specifically apply this code to my case, make the font fatter? - Peter