tags:

views:

217

answers:

8

Hi all, 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.

A: 

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

Matt Huggins
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.
JacobM
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
please, try to be smart. It's a comment HTML. It has been stripped by markdown..
Jack
@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.
JeffH
It looks like you were flagged by the Smart Police too Matt.
Amir Afghani
The string "<!--" and "-->" are in fact part of the parent string. It appears that the Java indexOf() method is not seeing it. Now I have implemented the code in a standalone class and it works perfectly, however when I injected into my application, its as if its not there.
Binaryrespawn
@Binaryrespawn - What is input.length() ? Odds are *very* small that indexOf is behaving incorrectly. And what do you mean "the parent string"?
JeffH
@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.
Matt Huggins
A: 
Amir Afghani
please, try to be smart. It's a comment HTML. It has been stripped by markdown..
Jack
What kind of comment is 'try to be smart?' The OP did not indicate that this was some sort of HTML tag, and I don't do HTML that often, so does that mean I'm not smart?
Amir Afghani
It means that you should try to understand that there's something hidden.. if you are not sure about what you are writing just don't write it :)
Jack
+3  A: 
System.out.println("!Chartered Certified<!--lol--> Accountants (ACCA)".indexOf("-->"));

prints 27

So your input string must not be what you expect

Pyrolistical
+3  A: 
 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?

Jack
My cents on that `<` is actually an `<` and so on.
BalusC
@Binaryrespawn see comment above
Pyrolistical
A: 

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.

Diego Pereyra
A: 

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.

Justin Ardini
A: 

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()), " ");
}
fastcodejava
A: 

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.