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
  • 572
  • 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
  • 42,413
  • 12
  • 112
  • 138
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
  • 65,802
  • 45
  • 192
  • 282
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
  • 2,945
  • 2
  • 26
  • 52
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,435
  • 1
  • 26
  • 38