Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中从字符串中删除一些单词_Java_Android_String_Replace - Fatal编程技术网

如何在java中从字符串中删除一些单词

如何在java中从字符串中删除一些单词,java,android,string,replace,Java,Android,String,Replace,我在android平台上工作,我使用一个字符串变量填充html内容,然后我想删除一些单词(特别是-删除介于。标记之间的任何单词。有解决方案吗?尝试: String input = "...<head>..</head>..."; String result = input.replaceAll("(?si)(.*<head>).*(</head>.*)","$1$2"); 字符串输入=“……”; 字符串结果=input.replaceAll(“(

我在android平台上工作,我使用一个字符串变量填充html内容,然后我想删除一些单词(特别是-删除介于
标记之间的任何单词。有解决方案吗?

尝试:

String input = "...<head>..</head>...";
String result = input.replaceAll("(?si)(.*<head>).*(</head>.*)","$1$2");
字符串输入=“……”;
字符串结果=input.replaceAll(“(?si)(.*)。*(.*)”,“$1$2”);
String newHtml=oldHtml.replaceFirst(“(?s)()(.*?)”,“$1$3”);
说明:

oldHtml.replaceFirst(" // we want to match only one occurrance
(?s)                   // we need to turn Pattern.DOTALL mode on
                       // (. matches everything, including line breaks)
(<head>)               // match the start tag and store it in group $1
(.*?)                  // put contents in group $2, .*? will match non-greedy,
                       // i.e. select the shortest possible match
(</head>)              // match the end tag and store it in group $3
","$1$3");             // replace with contents of group $1 and $3
oldHtml.replaceFirst(//我们只想匹配一个发生率
(?s)//我们需要打开Pattern.DOTALL模式
//(.匹配所有内容,包括换行符)
()//匹配开始标记并将其存储在组$1中
(.*)//将内容放入组$2,.*)将匹配非贪婪,
//即,选择尽可能短的匹配项
()//匹配结束标记并将其存储在组$3中
“,“$1$3”);//替换为组$1和组$3的内容
另一种解决方案:)

String s=“起始页测试结束页”;
StringBuilder=新的StringBuilder;
删除(s.indexOf(“”+6,s.indexOf(“”));
System.out.println(builder.toString());

如果内容包含换行符(几乎可以肯定是这种情况),则将不起作用
oldHtml.replaceFirst(" // we want to match only one occurrance
(?s)                   // we need to turn Pattern.DOTALL mode on
                       // (. matches everything, including line breaks)
(<head>)               // match the start tag and store it in group $1
(.*?)                  // put contents in group $2, .*? will match non-greedy,
                       // i.e. select the shortest possible match
(</head>)              // match the end tag and store it in group $3
","$1$3");             // replace with contents of group $1 and $3
String s = "Start page <head> test </head>End Page";
StringBuilder builder = new StringBuilder(s);
builder.delete(s.indexOf("<head>") + 6, s.indexOf("</head>"));

System.out.println(builder.toString());