Results 1 to 7 of 7
  1. #1
    sleeper0110 is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Inserting a newline into a string

    Problem: I need to insert a newline into a string where a letter follows a non-letter.
    Example String: A63B432

    I want to insert a newline after A63

    I only have the string class available to me.
    I'm familiar with regular expressions however I don't know how to use the String class to insert a newline.

    I'm guessing the regular expression I'm looking for would be similar to:
    [\.0-9_][a-zA-Z]
    (for . OR didgit OR underscore followed by an alpha character)

    But how to insert a newline between the two?

    Any ideas?
    Thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Inserting a newline into a string

    Check out the different methods of the Pattern and Matcher classes (which are to do with regular expressions) and you may just get an idea yourself.

    Matcher (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    sleeper0110 is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Inserting a newline into a string

    Thanks, but as I've said in my original posting, I only have access to the String class.
    I can't use Matcher or Pattern.

    Anybody have ideas on how to do this with only the String class?

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

    Default Re: Inserting a newline into a string

    The String class has methods for accessing its characters. See the API doc.

    How will you determine if a char in the String is a letter or not?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sleeper0110 is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Inserting a newline into a string

    I was hoping I could use either [\.0-9_] or \W in the regular expression to represent a non letter character.

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

    Default Re: Inserting a newline into a string

    Is this a regular expression question?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    sleeper0110 is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Inserting a newline into a string

    I got it working using this:

    System.out.println(Str.replaceAll("([\\.0-9_])([a-zA-Z])", "$1\n$2" ));
    which will insert a newline character in between a non-letter followed by a letter.

Similar Threads

  1. inserting a character at multiple places in a string
    By DannyGroff in forum New To Java
    Replies: 2
    Last Post: 08-21-2013, 05:46 PM
  2. Replies: 6
    Last Post: 05-12-2012, 04:29 AM
  3. Problem inserting String into postgresql.
    By ciglesias in forum JDBC
    Replies: 6
    Last Post: 04-13-2011, 06:09 PM
  4. newline in a string
    By Billaguana in forum New To Java
    Replies: 13
    Last Post: 01-24-2011, 07:06 PM
  5. Replies: 0
    Last Post: 02-07-2008, 08:57 AM

Posting Permissions

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