Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Split a String in java using delimeters

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Split a String in java using delimeters

    Hello guys,

    I have a String as follows: "the|boy|is|good"

    now, I want to split this string using the "|" delimeter.

    I tried this :
    String line = "the|boy|is|good";
    line.split("|");

    but it didn't split it. Is there some regex for "|"?
    Anyone willing to help out here?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Split a String in java using delimeters

    Why not post a runnable example?

    Quite often, if you're using a Java special character in quotes, you need to escape it with '\'. To add to the complication, you also need to "escape the escape," so that the result is:

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

    As I suggested above, here's a runnable example:
    public class TestClass
    { 
        public static void main(String[] args)
        { 
            String line = "the|boy|is|good";
            String[] lineSplit = line.split("\\|");
     
            for ( String string : lineSplit )
            {
                System.out.println( string );
            }
        }
    }

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (August 30th, 2014)

Similar Threads

  1. Replies: 3
    Last Post: April 25th, 2013, 04:21 PM
  2. Split a String into Ints
    By spunkyk014 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 7th, 2013, 09:35 PM
  3. Split String and for loop issues. (Beginner problems)
    By AT-LOW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:02 PM
  4. Split a string into individual characters
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 03:55 PM
  5. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM

Tags for this Thread