Java remove Sub-string that includes quotes
String strLine = "";
try
{
BufferedReader b = new BufferedReader(new FileReader("html.txt"));
strLine = b.readLine();
} catch(Exception e)
{
e.printStackTrace();
}
String[] temp = strLine.split("<");
temp = temp[1].split(">");
String temp1 = ("<"+temp[0]+">");
strLine = strLine.replaceFirst(temp1,"");
System.out.println(strLine);
Basically I want to remove this string
<span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA">
from from the file that contains
<span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA">no'b?l</span>
However so far my code works only if the string contains no quotes. How can I fix this problem. I have tried using
.replaceAll("\\\"","\\\\\"");
but still failed.
Any help or info wi开发者_开发知识库ll be greatly apreciated.
Your problem is that replaceFirst accepts a regular expression, but you're feeding it an arbitrary string, that might contain all sorts of special characters that have a specific meaning in a regular expression. I don't think the quotes are your problem, but rather the question mark parentheses.
One way to work around this is to use the String#replace method, which accepts a string rather than a regular expression. That is, use the following line:
strLine = strLine.replace(temp1,"");
This differs from your code in that it replaces all the instances of temp1 in that line, and not just the first one, but I think you should be fine with that.
AFAIK replaceAll("///"","/////"");
would work if you escaped correctly: the escape character is \
, not /
. Try using that instead.
精彩评论