0

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

Mustafa
  • 592
  • 8
  • 17

4 Answers4

4

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);
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
2

It is quite simple to do this using regular expressions.

String src = "<title>Java Code</title>";
String dst = src.replaceAll("<.+?>", "");

System.out.println(dst);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

Since you specifically want to use chatAt(i), here is the algorithm,

  • Start traversing the string from the beginning.
  • If the character you encounter is an opening tag(<), start traversing the string until you find the closing tag (>). then check the next character, If it is (< ) , then repeat the same process again.
  • If the next character is not (<), Then start printing the string until you see another (<).
  • Then repeat step 2.
MoveFast
  • 3,011
  • 2
  • 27
  • 53
0

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.

Community
  • 1
  • 1
jackrabbit
  • 5,525
  • 1
  • 27
  • 38