Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: split strings

  1. #1
    aravindaj is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default split strings

    String str = "#11* 1# 2*12# 3"


    required o/p in array as

    #11
    * 1
    # 2
    *12
    # 3

    i.e splitting the strings for every 3rd string and storing it in an array of strings

    Please help as iam new bie to java

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: split strings

    Have a look at the substring( ... ) method in the String class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: split strings

    Also the indexOf() method could be useful
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: split strings

    Hi,

    this can easily be done with String.Split. You need a regular expression that makes sure that the next hit starts where the first ends so you need a \G in there.
    So a solution could be a split call like this: split("(?<=\\G.{3})")

    There are also a lot of hits when you search for this in google. One of these hits is:
    regex - Split string to equal length substrings in Java - Stack Overflow

    But you can also code it yourself with simple substring calls inside a while loop (as others mentioned already).

    With kind regards,

    Konrad

  5. #5
    aravindaj is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: split strings

    Thanks Konrad.
    ("(?<=\\G.{3})") worked :)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: split strings

    If this is for a class, better make certain you can explain it. Negative and positive lookahead/lookbehind assertions coupled with zero-width modifiers can be tricky and to some folks, rather advanced.

    Regards,
    Jim
    kneitzel likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. CSV and split help
    By LostinThoughts in forum New To Java
    Replies: 10
    Last Post: 09-30-2012, 03:01 AM
  2. How to use split?
    By jeff_limsy in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 09:23 AM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09: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
  •