Solved

search for coded URL string "%06"

Posted on 2002-06-12
5
176 Views
Last Modified: 2010-03-31
I've got a decoded URL string, and in that string there is an not-displayable-character encoded representing "%06",
question is: How do search for that not displayable charcter where I want to use it as a delimiter in methods like StringTokenizer(String str, String delim)?
Thanks.
0
Comment
Question by:tomboman
5 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 7074850
you can decode it first and then it will take a single char.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7074878
I don't think that you can use
String.indexOf() with such a character. Instead of this I suggest using
StringTokenizer(String str, String delim = "%06")
with the encoded string and after that - decoding the returned tokens
0
 
LVL 35

Expert Comment

by:girionis
ID: 7074979
 The following should work:

String s = "hello%06world";
java.util.StringTokenizer st = new java.util.StringTokenizer(s, "%06");
while (st.hasMoreTokens())
{
     System.out.println(st.nextToken());
}

  and the output should be:

hello
world

  Hope it helps.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 7076145
Unfortunately, that example will also treat all occurrences of any one of these characters as a delimiter. This is because the delim argument to StringTokenizer really specifies a set of characters that act as delimiters, not a single group of characters that must be completely matched.

In a recent JavaWorld article that discussed this 'pitfall' (http://www.javaworld.com/javaworld/jw-09-2000/jw-0922-javatraps_p.html) the following code solution was given as an alternative:

public static String[] tokenize(String input, String delimiters, boolean delimiterAsGroup) {
    Vector v = new Vector();
    String toks[] = null;

    if (!delimiterAsGroup) {
        StringTokenizer t = new StringTokenizer(input, delimiters);
        while (t.hasMoreTokens())
            v.addElement(t.nextToken());
    } else {
        int start = 0;
        int end = input.length();

        while (start < end) {
            int delimIdx = input.indexOf(delimiters,start);
            if (delimIdx < 0) {
                String tok = input.substring(start);
                v.addElement(tok);
                start = end;
            } else {
                String tok = input.substring(start, delimIdx);
                v.addElement(tok);
                start = delimIdx + delimiters.length();
            }
        }
    }

    int cnt = v.size();
    if (cnt > 0) {
        toks = new String[cnt];
        v.copyInto(toks);
    }
     
    return toks;
}

Best regards,
Jim Cakalic
0
 

Author Comment

by:tomboman
ID: 7077457
Very detailed answer jim, thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
javap not working 8 58
login jsp example 24 65
maven disable workspace resolution 1 21
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question