2

this must be quite simple but I am having great difficulty. You see I am trying to find a string within another string as follows.

            e = input.indexOf("-->");
            s = input.indexOf("<!--");
            input = input.replace(input.substring(s, e + 3), " ");

The integers e and s are returning -1 in that it was not found and this is causing the replace method to fail. The test string I am using is "Chartered Certified<!--lol--> Accountants (ACCA)". I tried to creat a new string object and pass in the string as an argument as follows

e=input.indexOf(new String("<!--"));

This yielded the same result. Any ideas ?

This is a stand alone piece of code I wrote and it works perfectly.

public static void main(String[] args) {
    int e = 0;
    int s = 0;
    while (e != -1) {
        //input.replace("\"", "\'");
        e = input.indexOf("-->");
        s = input.indexOf("<!--");
        input = input.replace(input.substring(s, e + 3), " ");
        e = input.indexOf("-->");
        System.out.println(input);
    }
}

But I cannot seem to see why it fails when i use this logic in my action class.

5
  • 1
    There's no match to the strings you're looking for. Did the comments get parsed out in formatting your post? Commented Mar 31, 2010 at 17:41
  • try to check your input string Commented Mar 31, 2010 at 17:44
  • 2
    Can you show the code where you set the value of input? Commented Mar 31, 2010 at 17:47
  • 1
    How should foo --> bar <!-- foo <!-- bar --> foo --> bar <!-- foo be parsed? Commented Mar 31, 2010 at 17:47
  • To be clearer on BalusC point: You need to consider illegally formatted comments Commented Mar 31, 2010 at 17:51

7 Answers 7

3
System.out.println("!Chartered Certified<!--lol--> Accountants (ACCA)".indexOf("-->"));

prints 27

So your input string must not be what you expect

Sign up to request clarification or add additional context in comments.

Comments

3
 String input = "Chartered Certified<!--lol--> Accountants (ACCA)";
 int e = input.indexOf("-->");
 int s = input.indexOf("<!--");

 System.out.println(e+" "+s);

yields

26 19

so I think that there's an error somewhere else, is there other code in the middle?

2 Comments

My cents on that < is actually an &lt; and so on.
@Binaryrespawn see comment above
1

The string "Chartered Certified Accountants (ACCA)" does not contain "-->" or "<!--", so e and s will always be -1.

7 Comments

Actually, the string he posted did have them, but they were interpreted as HTML comments and not displayed. I added a code block to show them.
Basically I am trying to manually strip away any characters enclosed between these two comments in java. befoer even hitting the jsp. So its strictly java logic. The indexOf() method returns -1 because the string was not seen by java, but it is part of the string.
@Binaryrespawn - Elaborate in your question on: "the string was not seen by java, but it is part of the string". Java appears to be working correctly.
It looks like you were flagged by the Smart Police too Matt.
@Jack - I would assume a coding site like StackOverflow would know how to convert less than and greater than signs to their escaped equivalents, no need to be a dick.
|
0

Maybe you are obtaining the string from a sort of xml parser and hides the commented string on rendering. Check that the input string just before the indexOf call really has the '<!--' and '-->' strings inside.

Comments

0

I ran the test real quick using a command-line argument. It worked just fine. Here's the code/results:

public static void main(String[] args) {
    String input = args[0];
    System.out.println(input);
    int e = input.indexOf("-->");
    int s = input.indexOf("<!--");
    input = input.replace(input.substring(s, e + 3), "");
    System.out.println(input);
}

Output:

Chartered Certified<!--lol--> Accountants (ACCA)
Chartered Certified Accountants (ACCA)

If you were passing input as a command-line argument, make sure it is in quotes, or else input will be set to Chartered because of the spaces.

Comments

0

This code should work, there is some other problem. You should code it for safety though as shown below.

e = input.indexOf("-->");
s = input.indexOf("<!--");
if (e > -1 && s > -1 && e > s + 4) {
    input = input.replace(input.substring(s, e + "-->".length()), " ");
}

Comments

0

Your code seems OK. So if it fails. it may be that the string it parses is not what you think it is. Where does the string come from? Try printing the string just before parsing it to see what it actually is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.