AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String '

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
zku
Guest





PostPosted: Thu Jul 24, 2003 8:18 am    Post subject: String ' Reply with quote



Hi,

I have a string s = "1 '2 '3 4 '5";

How can I retrieve the string from single quote to another single quote and
kept the escape letter ""?

i want the String retrieved = "'2 '3 4 '";

any idea?


Back to top
Manish Jethani
Guest





PostPosted: Thu Jul 24, 2003 7:59 pm    Post subject: Re: String ' Reply with quote



zku wrote:

Quote:
I have a string s = "1 '2 '3 4 '5";

How can I retrieve the string from single quote to another single quote and
kept the escape letter ""?

i want the String retrieved = "'2 '3 4 '";

class a
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(foo(args[0]));
}

static String foo(String s)
{
return foo(s, 0);
}

static String foo(String s, int beginIndex)
{
return foo(s, beginIndex, s.length());
}

static String foo(String s, int beginIndex, int endIndex)
{
StringBuffer s1 = new StringBuffer(s.length());
boolean e = false, q = false;
loop:
for (int i = beginIndex; i < endIndex; i++) {
char c = s.charAt(i);
if (!e) {
switch (c) {
case '\':
e = true;
break;
case ''':
q = !q;
if (!q) break loop;
break;
default:
if (q) s1.append(c);
}
} else {
if (q) s1.append(c);
e = false;
}
}
if (!q)
return s1.toString();
else
return null;
}
}

$ java a "1 '2 '3 4 '5"
1 '2 '3 4 '5
2 '3 4

But you're better off with regular expressions Wink Also ask on
comp.lang.perl for a regex-based solution.

-Manish

--
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488


Back to top
Roedy Green
Guest





PostPosted: Thu Jul 24, 2003 9:05 pm    Post subject: Re: String ' Reply with quote



On Thu, 24 Jul 2003 16:18:55 +0800, "zku" <zku (AT) tm (DOT) net.my> wrote or
quoted :

Quote:
"'2 '3 4 '";

see http://mindprod.com/jgloss/literals.html
you want to quote the ' and the itself.

so

"'2 \'3 2 '"

If this boggles the mind, see the quoter amanunesis for help.
http://mindprod.com/quoter.html Tell it you want your strings
converted to Java.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.