469,306 Members | 2,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

pattern match a question mark

12
hello,

I am trying to split a string on a question mark - obviously this is a special character, but using a backslash to cope with this does not seem to work.

Expand|Select|Wrap|Line Numbers
  1. String loc="firstpart?secondpart";
  2. String[] l = loc.split ("/?");
  3. String loc = l[0];
loc does not end up as "firstpart" which is what I am after.

Please help !

basm101
Nov 29 '07 #1
5 17854
r035198x
13,262 8TB
hello,

I am trying to split a string on a question mark - obviously this is a special character, but using a backslash to cope with this does not seem to work.

Expand|Select|Wrap|Line Numbers
  1. String loc="firstpart?secondpart";
  2. String[] l = loc.split ("/?");
  3. String loc = l[0];
loc does not end up as "firstpart" which is what I am after.

Please help !

basm101
You escape special characters using the backslash \.Hoever, the backslash character is itself a special character so you need to escape it as well. Your code thus should be
Expand|Select|Wrap|Line Numbers
  1. loc.split("\\?");
Nov 29 '07 #2
Take also a look at some useful methods defined in java.util.StringTokenizer
for example
Expand|Select|Wrap|Line Numbers
  1. boolean hasMoreTokens()
  2.           Tests if there are more tokens available from this tokenizer's string.
  3. String     nextToken() 
  4.           Returns the next token from this string tokenizer.
  5.  
with these 2 methods you can manage your temporary results.
Nov 29 '07 #3
r035198x
13,262 8TB
Take also a look at some useful methods defined in java.util.StringTokenizer
for example
Expand|Select|Wrap|Line Numbers
  1. boolean hasMoreTokens()
  2.           Tests if there are more tokens available from this tokenizer's string.
  3. String     nextToken() 
  4.           Returns the next token from this string tokenizer.
  5.  
with these 2 methods you can manage your temporary results.
I wouldn't use it.
Read the API specs for it here.

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the splitString or the java.util.regex package instead.
Nov 29 '07 #4
basm101
12
Expand|Select|Wrap|Line Numbers
  1. loc.split("\\?");
  2.  
worked a treat. Thanks !

basm101
Nov 30 '07 #5
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. loc.split("\\?");
  2.  
worked a treat. Thanks !

basm101
I hope you also got the concept.
Try to match \ itself and see what you need to use.
Nov 30 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Fabian | last post: by
9 posts views Thread by Tjerk Wolterink | last post: by
1 post views Thread by Tony Johansson | last post: by
5 posts views Thread by Terry Olsen | last post: by
19 posts views Thread by konrad Krupa | last post: by
3 posts views Thread by konrad Krupa | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.