I want to know if there is any way to print the underlined text on console using java String class.
Asked
Active
Viewed 5.7k times
5
-
why not System.out.println(/*..*/); – Maxim Shoustin Oct 14 '13 at 06:55
-
2There already is an answer to this question: http://stackoverflow.com/questions/5062458/font-settings-for-strings-in-java – PrR3 Oct 14 '13 at 06:56
-
PrP3; that's helpful - I wasn't aware of that. – Bathsheba Oct 14 '13 at 06:58
-
2None of this will work in Windows command prompt. It's specific to full-fledged terminal emulators (the norm on *nixes, including Mac). – Marko Topolnik Oct 14 '13 at 07:49
4 Answers
5
You can only rely on how the String is interpreted by your console. For example, in bash
, you can do something like inserting your string between escape sequences \033[1m
and \033[0m
(for bold):
\033[1mInsert your String here\033[0m
You should read the documentation of your favorite console.

Arnaud Denoyelle
- 29,980
- 16
- 92
- 148
2
I'd have a look at the tput
command as outlined in this answer, but it will make your code a bit unreadable as it will look like this
System.out.println("...")
// execute tput
System.out.println("underlined text")
// execute tput
System.out.println("...")
Also, this page may help
Notice I'm assuming you aim for bash like terminal

Community
- 1
- 1

David Rabinowitz
- 29,904
- 14
- 93
- 125
2
I used Netbeans IDE v12.0
String txt="Welcome Back";
String utxt = String.join("\u0332",n.split("",-1));
System.out.println(utxt);
Output
W̲e̲l̲c̲o̲m̲e̲ ̲B̲a̲c̲k̲

Khalid
- 41
- 4
-3
On my IDE you can also use like the below Mentioned :
System.out.println("Underlined Text");
System.out.println("_______________")

Raymond Chenon
- 11,482
- 15
- 77
- 110

Harish Kumar
- 1
- 2