Solved

String Manupulation.

Posted on 2004-10-21
224 Views
Last Modified: 2008-02-01
In a string whenever the charcters "DC#" appears, I want to extract the next 4 characters.
So for example, if the string was "LAWRENCE DC#1234 MORE TEXT" I want to extract "1234".
How can this be done ?
0
Question by:apparition
    6 Comments
     
    LVL 11

    Expert Comment

    by:cjjclifford
    split the string on the '#' and take the first 4 characters of the second section of the split...

    mystring.split( "#" ).substring( 0, 4 )
    0
     
    LVL 5

    Author Comment

    by:apparition
    I need to make sure the "#" is preceded by "DC"
    so if my string was  "BLA # BLA DC#1234 BLA" your solution won't work.
    0
     
    LVL 11

    Expert Comment

    by:cjjclifford
    sorry, of course split() returns array, and also, substring() takes the second argument as the index to stop the substring, so 3 will be the fourth character...

    so...

    mystring.split( "#" )[1].substring( 0, 3 )
    0
     
    LVL 11

    Accepted Solution

    by:
    ok, so split on the "DC#", and check the result before doing the substring.

    String[] bits = mystring.split( "DC#" );
    if( bits.length == 2 ) {
        subs = bits[1].substring( 0, 3 );
    }

    you can also do this in a regular expression...
    0
     
    LVL 5

    Author Comment

    by:apparition
    again won't work for a string like  "BLA # BLA DC#1234 BLA"
    0
     
    LVL 11

    Expert Comment

    by:cjjclifford
    thanks - out of interest, the regex impl is along the following lines:

    import java.util.regex.*;
    ...
    Matcher matcher = Pattern( ".*DC#(....).*" ).matcher( mystring );
    if( matcher.matches() ) {
        substring = matcher.group(1);
    }

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Suggested Solutions

    Title # Comments Views Activity
    countClumps  challenge 10 53
    advance java 4 22
    factorial example challenge 10 35
    IT Company 5 38
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now