Java String Split with Pipe Character Not Working - Solution

If you are working on Java, you might have run into this issue when you try to split a string based on a pipe character ( "|"...




If you are working on Java, you might have run into this issue when you try to split a string based on a pipe character ("|"). It simply won't work. Split method in Java takes regex as an argument. There are certain characters including the pipe character which has special meaning in regex and are called meta characters. Here is a list of Java Regex meta characters

( ) [ ] { { \ ^ $ | ? * + . < > - = !


There are three ways to resolve this issue,

1. Use Pattern.quote()

Pattern is a class for handling regex expressions in Java. Pattern.quote() returns a literal pattern String for the specified String. Metacharacters or escape sequences in the input sequence will be given no special meaning.

String[] values =values.split(Pattern.quote("|"));

2. Use Escape Characters

This is simple. Just escape the pipe character with double slash.

String[] values = values.split("\\|");

3. Use StringTokenizer to Split

You can use StringTokenizer to split strings and avoid all regular expression issues.

public static String[] splitUsingTokenizer(String original, String delimiters) 
{
     StringTokenizer token = new StringTokenizer(original, delimiters);
     ArrayList<String> list = new ArrayList<String>(original.length());
     while(token.hasMoreTokens())
     {
       list.add(token.nextToken());
     }
     return list.toArray(new String[0]);
}



Keep yourself subscribed for getting programmingfree articles delivered directly to your inbox once in a month. Thanks for reading!

Subscribe to GET LATEST ARTICLES!


Related

Java 734647647001795499

Post a Comment

  1. Thanks for that, it was a lifesaver

    ReplyDelete
  2. Thank you for posting very useful tips! I found your website perfect for my needs.I always like your article because you have provide every time informative post..Essay Editor Service

    ReplyDelete
  3. wow, awesome blog.Thanks Again. Much obliged 먹튀검증 I believe you are the person I was looking for 먹튀검증

    ReplyDelete
  4. Are you looking for custom packaging for your CBD boxes? Well, look no further Because Custom CBD Boxes is here for you. There are two major reasons as to why you should use a customized box for your CBD boxes; market size and CBD unique position in society.

    ReplyDelete
  5. Your site is very good and informational for me. We are the best suppliers of Custom Printed CBD Boxes in the UK. If anyone has interest in them please visit our website for more products.

    ReplyDelete
  6. Really nice and informative blog, keep it up. Thanks for sharing and I have some suggestions.
    Please Visit thanks .

    ReplyDelete
  7. The way you write is awesome you know the techniques that how to attract people for your writing its appreciated. If anybody needs excellent quality packaging and packaging services so Packaging Printing is making the good quality any type of Boxes, in any style material, size shape and color. Also Offering you free shipping and design support. Packaging Printing also Providing you the quality material of Soap Boxes and Soap Packaging with unique designs and fabulous Printing.

    ReplyDelete
  8. On the off chance that you are seeing some issue with your Brother Printer as it is associated yet not printing, the explanation could be anything behind it. Nonetheless, you can fix this issue by doing some essential investigating. What's more, to assist you with this, this post has presented to you some data and guidelines to follow. Fix Brother printer not printing problemThus, go thoroughly.To start with, you should check for the paper stuck issue in your Brother Printer. There are times when you don't used the printer for quite a while and dust gets gathered in the printer that prompts trouble.Also, you need to check the ink filled in the cartridge whether it is filled deficiently or void. For this situation, you will not get any printer issues like Brother Printer associated yet not printing.In expansion, an unfilled or half-filled cartridge causes the issue yet additionally when the ink is filled more than the necessary ink it likewise raises a ruckus in getting prints.When you fill ink in the cartridge more than the necessary sum, it might flood and can impede the printer head.

    ReplyDelete
  9. Amazing product thanks for sharing with us It is very informative. If you need any type of boxes you can visit the link.
    Lingerie packaging Boxes wholesale
    Holiday Party boxes Alaska

    ReplyDelete
  10. Howdy! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Cheers! 메이저놀이터

    ReplyDelete

emo-but-icon

SUBSCRIBE


item