• Post Reply Bookmark Topic Watch Topic
  • New Topic

coloring the words in a String  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 498
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to set the color of the words in a string.
for example:
I want to make RED all the "the" of the above string . The word "the" I can find using REGEX. But how can i make them colorful ?
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Java String is just a 'string' of characters. There is no formatting behaviour baked in there.

So when you say you want to make all "the" words be red you need to tell us where you want them to display in red? Console? Browser? Other?
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some Swing components which can accept basic HTML tags. You can then change the String to read, "<html> <color=red>the</color> wea<color=red>the</color>r is <color=red>the</color>ir lea<color=red>the</color>r </html>"

Now, isn't that horrible! I think it is a JLabel you want but I am not sure.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That cannot work in Java® because it is not Java® code. Is it JS or similar?

And welcome to the Ranch
 
Puspender Tanwar
Ranch Hand
Posts: 498
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell , Tim
I want to do this on console
Actually I am preparing for OCP, currently learning String Processing. So I thought why not to code a function which shows the affected area in String when we process character classes on them (\B, \w, ^ etc .)
It will make me easy to understand and fast too (I thought ).
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar question was asked not so long ago. Perhaps the links provided there might be of some use to you.
 
Puspender Tanwar
Ranch Hand
Posts: 498
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Tim, but I found a new way to perform my task .

output is : THE leather in THEir coat made her THEseethe
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That cannot work in Java® because it is not Java® code. Is it JS or similar?

Never programmed PHP?
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's not changing the colour, which is what you asked about at the beginning.

With your new solution, what if your input string is:

What then?
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:. . .
String p="\\bthe";
. . .
That is different; that only finds words beginning the not all instances of the.
Don't use the + operator in multiple lines on Strings: really poor performance. Use a StringBuilder. You can insert things into a StringBuilder instead, if you wish.
 
Puspender Tanwar
Ranch Hand
Posts: 498
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Puspender Tanwar wrote:. . .
String p="\\bthe";
. . .
That is different; that only finds words beginning the not all instances of the.
Don't use the + operator in multiple lines on Strings: really poor performance. Use a StringBuilder. You can insert things into a StringBuilder instead, if you wish.

Hey Tim & Compbell, my purpose for this post was not to ask about the REGEX. Purpose was to write a code which will help me to understand how REGEX works. And instead of making the words colorful, i made them CAPS.
@campbell, I was expecting someone to give advice on improving my code. Thank you
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender,
Basically for what you are asking is console formatting codes, you can do this by including ANSI Escape codes in your strings at the appropriate places--in much the same way as HTML works to format your browser content.

ANSI Escape Codes

Back in the day before HTML and the Internet, I wrote a package for VT-100, and a few other terminals, that basically allowed you to format anything according to how you wanted to view your screen.

Puspender Tanwar wrote:
Campbell Ritchie wrote:
Puspender Tanwar wrote:. . .
String p="\\bthe";
. . .
That is different; that only finds words beginning the not all instances of the.
Don't use the + operator in multiple lines on Strings: really poor performance. Use a StringBuilder. You can insert things into a StringBuilder instead, if you wish.

Hey Tim & Compbell, my purpose for this post was not to ask about the REGEX. Purpose was to write a code which will help me to understand how REGEX works. And instead of making the words colorful, i made them CAPS.
@campbell, I was expecting someone to give advice on improving my code. Thank you
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:thanks Tim, but I found a new way to perform my task .

You could do it slightly more compact, viz:or indeed just:
Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!