用颜色替换字符串中的字符
问题描述:
我一直在寻找一段时间,如何在发送时更改控制台的颜色。用颜色替换字符串中的字符
我给你约我想做的事一个例子:
public String color_convert(String toConvert){
toConvert = toConvert replace -> &0 with black text
toConvert = toConvert replace -> &1 with dark blue text
return toConvert;
}
所以字符串应该是什么样子,例如
String colorConverted = color_convert("&0This is black&1 and this is blue");
它应该显示更多或更少的像这个: Image
答
在通常不可能的字符串中,需要颜色/字体属性与文本平行。
不过的Java Swing和JavaFX的也可以代表HTML文本:
public String color_convert(String toConvert) {
String html = "<html><span>"
+ toConvert
.replace("&0", "</span><span style=\"color: black\">")
.replace("&1", "</span><span style=\"color: blue\">")
.replace("\n", "<br>") // line break
+ "</span>";
return html;
}
+0
不,它将返回并且不能用作颜色:P –
我相信这样做的最常见的方式是使用HTML样式的文本。但这一切取决于控制台如何设置为工作 – ControlAltDel
如果控制台支持ANSI转义码,则可以将它们嵌入到字符串中。请参阅https://en.wikipedia.org/wiki/ANSI_escape_code – dazedandconfused