Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


String Manupulation.

Posted on 2004-10-21
Medium Priority
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 ?
Question by:apparition
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 11

Expert Comment

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

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

Author Comment

ID: 12371526
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.
LVL 11

Expert Comment

ID: 12371527
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...


mystring.split( "#" )[1].substring( 0, 3 )
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 11

Accepted Solution

cjjclifford earned 500 total points
ID: 12371553
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...

Author Comment

ID: 12371557
again won't work for a string like  "BLA # BLA DC#1234 BLA"
LVL 11

Expert Comment

ID: 12371776
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);


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

610 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