I have a string and i'd like to remove all tags with < and >
For example:
in the String
<title>Java Code</title>
will be
Java Code
and
<pre><font size="7"><strong>Some text here
</strong></font><strong>
will be
Some text here
How can it be done with using charAt(i)? Thanks in advance
How can it be done with using charAt(i)?
Here is how:
public static void main(String[] args) {
String s = "<pre><font size=\"7\"><strong>Some text here\n\n</strong></font><strong>";
String o = "";
boolean append = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '<')
append = false;
if (append)
o += s.charAt(i);
if (s.charAt(i) == '>')
append = true;
}
System.out.println(o);
}
It is quite simple to do this using regular expressions.
String src = "<title>Java Code</title>";
String dst = src.replaceAll("<.+?>", "");
System.out.println(dst);
Since you specifically want to use chatAt(i), here is the algorithm,
with charAt
, you could loop over all the characters in you string, removing everything from < until the next >. However, your string could contain non-ASCII UTF code points, which could break this approach.
I would go with a regex, something like
String someTextHere = "...";
String cleanedText = someTextHere.replaceAll( "<[^>]*?>", "" );
However, let me also point you to this question , which lists concerns with the regex approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.