如何在Java中提取特定的子字符串

如何在Java中提取特定的子字符串,java,regex,substring,Java,Regex,Substring,我有一个字符串,我定义为 String string = "<html><color=black><b><center>Line1</center><center>Line2</center></b></font></html>"; String String=“Line1Line2”; 我应用于一个JButton,在它上面得到两行文本,这非常有效。调用JButton.ge

我有一个字符串,我定义为

String string = "<html><color=black><b><center>Line1</center><center>Line2</center></b></font></html>";
String String=“Line1Line2”;
我应用于一个JButton,在它上面得到两行文本,这非常有效。调用JButton.getText()方法时,它返回整个字符串。我想要的是获取它返回的字符串,并从中获取字符串“Line1Line2”。(所以我想删除所有HTML代码,只获取屏幕上显示的文本)

if(string.contains("<html>"))
    {
        string.replace("<html>", "");
    }
if(string.contains(“”)
{
字符串。替换(“,”);
}
然后对所有其他的“”执行相同的操作,但是如果我打印字符串,我仍然可以得到全部内容。我认为使用正则表达式比手动删除所有“”更好,但我不知道如何删除

任何帮助都将不胜感激

string.replace()
不会修改字符串:字符串是不可变的。它返回一个新字符串,其中替换已完成

所以你的代码应该是

if (string.contains("<html>")) {
    string = string.replace("<html>", "");
}
if(string.contains(“”){
string=string.replace(“,”);
}
string.replace()
不会修改字符串:字符串是不可变的。它返回一个新字符串,其中替换已完成

所以你的代码应该是

if (string.contains("<html>")) {
    string = string.replace("<html>", "");
}
if(string.contains(“”){
string=string.replace(“,”);
}

字符串
是不可变的,因此
字符串#替换
不会更改
字符串
,而是返回更改后的
字符串

string = string.replace("<html>", "");
string=string.replace(“,”);

依此类推。

String
是不可变的,因此
String#replace
不会更改
字符串,而是返回更改后的
字符串

string = string.replace("<html>", "");
string=string.replace(“,”);

诸如此类。String也有一个
replaceAll()
方法

您可以尝试
string.replaceAll(“,”)


还请记住,java中的字符串是不可变的,此操作将返回一个新字符串和结果

String
还有一个
replaceAll()
方法

您可以尝试
string.replaceAll(“,”)

还请记住,java中的字符串是不可变的,此操作将返回一个新字符串和结果

再次感谢!这就是我需要的。这是我现在欠你的两次:)谢谢(再次)!这就是我需要的。这是我现在欠你的两倍:)