Help using String.split(..)

  • Thread starter Miss. Michelle Heigardt
  • Start date
M

Miss. Michelle Heigardt

Hi, don't reply to this e-mail message it doesn't exist and is to stop
my real address getting spammed.

The following Java displays

c:\dir1\dir2\fred
txt

which is what I expect. Here is the Java.

public static void main(String[] args)
{
String s="c:\\dir1\\dir2\\fred.txt";
String[] sa=s.split("[.]");
for (int i=0;i<sa.length;i++)
System.out.println(sa);
}

But I want to display

c:
dir1
dir2
fred.txt

How do I use String.split to do this. When I try this I get the error
message.


public static void main(String[] args)
{
String s="c:\\dir1\\dir2\\fred.txt";
String[] sa=s.split("[\\]");
for (int i=0;i<sa.length;i++)
System.out.println(sa);
}

[\]
^
at java.util.regex.Pattern.error(Pattern.java:1489)
at java.util.regex.Pattern.clazz(Pattern.java:2002)
at java.util.regex.Pattern.sequence(Pattern.java:1546)
at java.util.regex.Pattern.expr(Pattern.java:1506)
at java.util.regex.Pattern.compile(Pattern.java:1274)
at java.util.regex.Pattern.<init>(Pattern.java:1030)
at java.util.regex.Pattern.compile(Pattern.java:777)
at java.lang.String.split(String.java:1795)
at java.lang.String.split(String.java:1838)
at mypackage7.Class1.main(Class1.java:19)


Thank you
Michelle. I am using Java 1.4.
 
I

Ike

Backslashes are escaped...so I think you want to use:
String[] sa=s.split("[\\\\]"); //4 backslashes
-or maybe even-
String[] sa=s.split("[\\\\\\]"); //6 backslashes

try them both - one should work.

Recently, I found that if I wanted to split on, say "}{" I had to speciy
this as "\\}\\{"

-Ike
 
A

Andrew Thompson


Hi. Could you post questions of this nature to comp.lang.java.help *
rather than c.l.j.programmer in future?

* <http://www.physci.org/codes/javafaq.jsp#cljh>

<sscce>
class TestSplit {
public static void main(String[] args)
{
String s="c:\\dir1\\dir2\\fred.txt";
/* a RegEx pattern inside Java source
needs to be 'double escaped' */
String[] sa=s.split("[\\\\]");
for (int i=0;i<sa.length;i++)
System.out.println(sa);
}
}
</sscce>

HTH
 
C

Chris Smith

Ike said:
Backslashes are escaped...so I think you want to use:
String[] sa=s.split("[\\\\]"); //4 backslashes
-or maybe even-
String[] sa=s.split("[\\\\\\]"); //6 backslashes

try them both - one should work.

You want four backslashes. Rather than programming by trial and error,
it's nice to understand why something is required. Trial and error
eventually fails you and causes indeterminate bugs to pop up for your
customers; not a good thing.

In this case, it's because Java String literals require backslashes to
be escaped, and regular expressions also require the same thing. So the
literal "[\\\\]" is actually only four characters long: [, \, \, and ]
in that order. This sequence of characters is then passed to the regexp
engine, which interprets the first backslash as an escape, so that the
pattern matches a backslash character.

An interesting implication is that if you were reading patterns from a
text file instead of writing them in String literals, then the second
level of encoding would not be necessary, so you would just write [\\]
instead of [\\\\].

Note that the character class is also unnecessary, so you could have
written the following instead of the code above:

String[] sa = s.split("\\\\");

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Andrew Thompson said:
Hi. Could you post questions of this nature to comp.lang.java.help *
rather than c.l.j.programmer in future?

* <http://www.physci.org/codes/javafaq.jsp#cljh>

Andrew, I'd appreciate it if you don't represent your own irritability
toward easy questions as an accepted behavior of this newsgroup. The
fact is that there's no clear definition of an "advanced" technical
question, as this thread demonstrates. Have you appointed yourself as
the judge?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

Andrew, I'd appreciate it if you don't represent your own irritability
toward easy questions as an accepted behavior of this newsgroup.

Just out of curiosity, you judge this as 'irritable'?

"Hi. Could you post questions of this nature to comp.lang.java.help *
rather than c.l.j.programmer in future?"
Have you appointed yourself as the judge?

ditto?
 
C

Chris Smith

Andrew Thompson said:
Just out of curiosity, you judge this as 'irritable'?

"Hi. Could you post questions of this nature to comp.lang.java.help *
rather than c.l.j.programmer in future?"

No, I read the web page you linked to; the one where you said "Short
sharp rebukes will be in store for any who dare post a lazy, ill-thought
out, badly researched or otherwise stupid question to c.l.j.programmer".
If that's true, it's a sad statement about the group.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

No, I read the web page you linked to; the one where you said "Short
sharp rebukes will be in store for any who dare post a lazy, ill-thought
out, badly researched or otherwise stupid question to c.l.j.programmer".
If that's true, it's a sad statement about the group.

On reflection. You're right, it needs adjusting.

How about..
"Short sharp rebukes *may* be in store for any who dare post a lazy,
ill-thought out, badly researched or otherwise stupid question
to c.l.j.programmer".

[ And note that my response to the OP in this instance was
neither sharp, nor a rebuke. ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top