Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

search for coded URL string "%06"

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
tomboman
Asked:
tomboman
1 Solution
 
heyhey_Commented:
you can decode it first and then it will take a single char.
0
 
Venci75Commented:
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
 
girionisCommented:
 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
 
Jim CakalicSenior Developer/ArchitectCommented:
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
 
tombomanAuthor Commented:
Very detailed answer jim, thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now