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
  11. bespoke packaging boxes At Bespoke Packaging UK we strongly believe in the interests of bespoke packaging, which has multiple benefits.

    ReplyDelete
  12. Your site is excellent. I was really touched by this post. I'm so delighted to locate the write-up I have actually been trying to find for a very long time. I covet the number of individuals sympathize with your writing. You discover a great deal of expertise from my writing.바카라사이트

    ReplyDelete
  13. Hello, I read the post well. 안전놀이터추천 It's a really interesting topic and it has helped me a lot. In fact, I also run a website with similar content to your posting. Please visit once


    ReplyDelete
  14. When I read your article on this topic, the first thought seems profound and difficult. There is also a bulletin board for discussion of articles and photos similar to this topic on my site, but I would like to visit once when I have time to discuss this topic. 안전토토사이트


    ReplyDelete
  15. Nice blog and informative sentence. I really like it. Keep sharing more with us.
    Data Science Courses

    ReplyDelete
  16. Thank you for your post. I sincerely thank you for your post. If you are interested in purchasing our products, you can contact us through this website Custom Packaging Boxes.

    ReplyDelete
  17. Decent blog and totally exceptional. You can improve however despite
    everything I say this perfect.Keep striving generally advantageous.
    고스톱

    ReplyDelete
  18. It was a very good post indeed. I thoroughly enjoyed reading it in
    my lunch time. Will surely come and visit this blog more often.
    스포츠토토

    ReplyDelete
  19. Very nice post. I just stumbled upon your blog and wanted to say
    that I’ve truly enjoyed surfing around your blog posts.
    After all I
    will be subscribing to your feed and I hope you write again very
    soon!
    일본야동

    ReplyDelete
  20. Incredible things you've generally imparted to us. Simply
    continue written work this sort of posts. The time which was
    squandered in going for educational cost now it can be utilized for
    studies. Thanks
    토토사이트

    ReplyDelete
  21. If you are a beginner, a java code helper could be your best friend. Whenever you want to learn codes or if you are looking for an idea what to put in your program, you can easily take ideas from this documentation. Here you will find all the details regarding the software you are working on. You can borrow ideas and codes as per your requirements. It is like your java assignment helper. Sometimes, the documentation holds so much information that the user gets flabbergasted. In such a situation, you can always search the internet for any kind of information or hint! So search the internet for any kind of java assignment help.

    ReplyDelete
  22. You can find all kinds and forms of fashion blogs because internet is brimming with them. 먹튀검증

    ReplyDelete
  23. it's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information 스포츠토토

    ReplyDelete
  24. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! 토토사이트

    ReplyDelete
  25. I appreciate dear this was an amazing article. I just love your post after reading and waiting for your next post also Visit us

    ReplyDelete
  26. Desert Snakes all need to be in habitats with certain things. Most of them live on land and they need fresh water to drink because they can get dehydrated. Some like more humid environments and some like dryer environments, but you don’t know which ones. There are many types of snakes, so this book will teach you about the different habitats of snakes and where they live, so that you can be more informed about them. This book includes pictures, lots of information and is perfect for children interested in snakes.

    ReplyDelete
  27. We provide all kinds of electrical solutions you will need to grow and flourish. Powerhouse Express brings efficient, reliable and accessible solutions to your doorstep.

    https://powerhouseexpress.com.pk/

    ReplyDelete
  28. Clipsal is now poised to be a major player in global markets and aims to be the number one electrical product brand name in the world.


    https://clipsal.com.pk//

    ReplyDelete
  29. The essay or research paper job is never limited to inputting the material. It's also essential to think about how much time and effort went into the study and analysis. Meanwhile, some deadlines are incredibly tight, making it nearly hard to complete a worthwhile product in such circumstances. Nonetheless, a qualified online paper helper and an expert team can promptly complete any job.

    ReplyDelete
  30. Watch and Download world's famous Turkish action drama Kurulus Osman Season 3 in English on link below
    👇
    Kurulus Osman Season 3

    Kurulus Osman Season 3 Episode 1
    On link below
    Kurulus Osman Season 3 Episode 1

    Crypto trading course
    Join on link below
    Crypto quantum leap

    YouTube course
    Be a professional YouTuber and start your carrier
    Tube Mastery and Monetization by matt

    Best product for tooth pain ,
    Cavity ,
    Tooth decay ,
    And other oral issues
    Need of every home
    With discount
    And digistore money back guarantee
    Steel Bite Pro

    ReplyDelete
  31. I think your website has a lot of useful knowledge. I'm so thankful for this website.
    I hope that you continue to share a lot of knowledge.
    This is my website.
    넷마블머니상

    ReplyDelete
  32. Because of the wonderful use of language, I thoroughly enjoyed reading it. https://cracksir.com/wavepad-sound-editor-crack/

    ReplyDelete
  33. I feel a lot more people need to read this, very good info! bitmain antminer s19

    ReplyDelete

  34. If you're still unsure whether you should pay someone to write my paper, consider the following tips to help you grasp some of the most important factors! Then you are at the right place. Our team of experts is highly proficient in providing online help for an write assignment.

    ReplyDelete
  35. Very useful info. Hope to see more posts soon! web design malta

    ReplyDelete
  36. This could be the right blog for everyone who is desires to be familiar with this topic. You already know much its practically not easy to argue along (not that I just would want…HaHa). You certainly put the latest spin with a topic thats been discussing for decades. Excellent stuff, just great! luxury bed

    ReplyDelete
  37. I used to be able to find good advice from your blog posts. perth podiatrists

    ReplyDelete
  38. I found useful information on this topic as Now i'm focusing on a company project. Thank you posting relative information and its currently becoming easier to complete this project ppc company australia

    ReplyDelete
  39. Your website is really cool and this is a great inspiring article. Thank you so much. Skip Hire Ulverston

    ReplyDelete
  40. I’ve found every article I’ve read very helpful. Good one, and keep it going. Shipping To FBA Warehouse From China

    ReplyDelete
  41. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. buy traffic

    ReplyDelete
  42. you are in point of fact a excellent webmaster. The site loading pace is amazing. It seems that you are doing any distinctive trick. In addition, The contents are masterwork. you have done a magnificent task on this subject! 토토사이트

    ReplyDelete
  43. This is a brilliant blog! I'm very happy with the comments!.. 먹튀검증사이트

    ReplyDelete
  44. "Excellent post. I will be dealing with a few of these isswues as well..

    " 먹튀검증

    ReplyDelete
  45. No matter what it is you are looking to move, we have the necessary tools to get the job done. Get your quotes now to see how we can help.
    In yoast seo be able unite the keyword you'd kind of thine submit yet web page in conformity with office because of into the iquire results.
    토토커뮤니티

    ReplyDelete
  46. surprised why this coincidence didn’t came about earlier! I bookmarked it
    I’ve found every article I’ve read very helpful. Good one, and keep it going.
    배팅사이트

    ReplyDelete
  47. Powerhouse is a novel concept of a superstore featuring the latest in Electrical and Electronic Products, High-Quality Lights, and World-Class Building Materials.

    https://powerhouse.com.pk/

    ReplyDelete

emo-but-icon

SUBSCRIBE


item