473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pattern match a question mark

12 New Member
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 18812
r035198x
13,262 MVP
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
MarshMallow
52 New Member
Take also a look at some useful methods defined in java.util.Strin gTokenizer
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 MVP
Take also a look at some useful methods defined in java.util.Strin gTokenizer
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 New Member
Expand|Select|Wrap|Line Numbers
  1. loc.split("\\?");
  2.  
worked a treat. Thanks !

basm101
Nov 30 '07 #5
r035198x
13,262 MVP
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
1563
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 href=\"(+?)\"(.*?)>%"; returns me results, but too many (that's why I wanted to use the first pattern, without any luck).
9
2232
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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:page="http://www.wolterinkwebdesign.com/xml/page" xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent"> <xsl:output method="xml" indent="yes"/>
1
5372
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 minimize the assumption other classes must make to use it. By building interface adaptation into a class, you eliminate the assumption that other classes see the same interface. Put another way, interface adaptation lets us incorporate our class...
4
9729
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 numeric value according to an arbitrary regular expression.
5
1648
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 perform certain functions. For example, I have a telnet client connected to an Apple II through the serial port. The user at the telnet terminal is using the BBS running on the Apple II just like the good ole days of dialup BBS's. I need to be...
4
1579
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 Value Name1 Value1
19
3162
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 /d/d/d/s/d/d in any text. There could be spaces in front or after the pattern (the nnn nn could be without spaces also) but it shouldn't pick it up in case like this 1234 56768
3
1623
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 pattern is going to make sure that I don't get match of ddd dd embeded in number. For example
4
2110
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
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.