 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
zku Guest
|
Posted: Thu Jul 24, 2003 8:18 am Post subject: String ' |
|
|
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
|
Posted: Thu Jul 24, 2003 7:59 pm Post subject: Re: String ' |
|
|
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 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
|
Posted: Thu Jul 24, 2003 9:05 pm Post subject: Re: String ' |
|
|
On Thu, 24 Jul 2003 16:18:55 +0800, "zku" <zku (AT) tm (DOT) net.my> wrote or
quoted :
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 |
|
 |
|
|
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
|
|