473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 18787
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

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

Similar topics

4
by: Fabian | last post by:
Hi all there, Sorry for this newbee question but how comes that the following pattern: $r = "%<td valign=top><a href=\"(+?)\"(.*?)>%"; does not return any result, while the pattern: $r = "%<a...
9
by: Tjerk Wolterink | last post by:
I have an xsl file wich xsl:includes this file: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"...
1
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
5
by: Terry Olsen | last post by:
Is there a good way to find a pattern of bytes/chars in a stream? I've got a serial port connected to a tcp port. I need to be able to catch a unique character string in the stream so that I can...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
3
by: konrad Krupa | last post by:
This message is a continuation of my previous post "Pattern Match" Doug - Thank you for your help. Doug Semler was able to solve my problem to some point but I still need some help. Doug's...
4
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.