如何在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中的字符串是不可变的,此操作将返回一个新字符串和结果再次感谢!这就是我需要的。这是我现在欠你的两次:)谢谢(再次)!这就是我需要的。这是我现在欠你的两倍:)