Solved

search for coded URL string "%06"

Posted on 2002-06-12
5
173 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now