Results 1 to 3 of 3
  1. #1
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default Using an asterisk to split a string.

    Uhm, yeah in short.

    Java Code:
    String[] splitUp = total.split ("*");
    gives me:

    Java Code:
    java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
    *
    ^
            at java.util.regex.Pattern.error(Unknown Source)
            at java.util.regex.Pattern.sequence(Unknown Source)
            at java.util.regex.Pattern.expr(Unknown Source)
            at java.util.regex.Pattern.compile(Unknown Source)
            at java.util.regex.Pattern.<init>(Unknown Source)
            at java.util.regex.Pattern.compile(Unknown Source)
            at java.lang.String.split(Unknown Source)
            at java.lang.String.split(Unknown Source)
            at PersonalizedLetter.main(PersonalizedLetter.java:16)
    So i though maybe i needed an escape sequence for an asterisk, but it said invalid escape sequence.

    Anyone know how i can split a string with *?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,764
    Blog Entries
    7
    Rep Power
    21

    Default

    You indeed have to escape the meaning of that metacharacter with a backslash; so you get \* for your regular expression; sadly enough the Javac compiler also uses backslashes in its String literals and the above isn't a legal java escape sequence; the solution is simple (but a bit ugly though): escape the special meaning of the backslash by adding yet another backslash character: "\\*".

    kind regards,

    Jos

  3. #3
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    aha,

    Awesome thanks.

    Makes sense, i knew the \\ escape but i didn't know exactly what * was for so i didn't think if the actual string was \* it would make a differance, but it did :)

    tyvm.

Similar Threads

  1. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 06:06 AM
  2. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 03:23 PM
  3. I wish to split a string xxx/yyyy/zzz so that ...
    By nmvictor in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 10:47 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •