0
Str = "abc|def|ghi^jkl|mno";
String[] Flds = Str.split("[|]"); 
//Flds[0] = "abc";
//Flds[1] = "def";
//Flds[2] = "ghi^jkl";
//Flds[3] = "mno";

now I want to know the sub fields for Flds[2], but none of the following are working -

String[] Flds = Str.split("[^]");

String[] Flds = Str.split("[^]]");

String[] Flds = Str.split("^");
1
  • Ehm, you are trying to split Str again, not Flds[2] Commented Nov 9, 2012 at 23:05

3 Answers 3

5

You should be splitting Flds[2]

String[] subFlds2 = Flds[2].split("\\^");

Also, ^ is a reserved character in regular expressions. You used [] to escape |, however, ^ also means something inside the [] brackets.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 To be more precise [] with ^ placed as first character is Negated Character Class for example [^bc] means every character besides b or c.
1

Double escape the carat:

Flds[2].split("\\^")

One escape for Java, then another for the regex.

Comments

0

Please note: String#split,accepts the parameter which is regular expression:

  public String[] split(String regex)

^ is special character in regex, which means beginning of the line. To use it as literal, please use a escape char before it e.g. below:

String[] Flds = Str.split("[\\^]");

Also I think, it should work fine without braces e.g.

String[] Flds = Str.split("\\^");

You may find more about regular expression character classes here: Regex Char Classes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.