• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Substring

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner in Java. I have a String as below:

public class Name {
public static void main(String[] args) {

String s = "John Doe";
String s1 = "Jane White";
System.out.println();

}
}
Now I want to print First name and last name seperately. How do I do this? I know I can write code as s.substring(0,4) or vice versa. But if you have a list of names you cant go about writing (0,3) or (0,5) simultaneously. You can seperate the FirstName and LastName from its space. How do you do that? Please help!

Thanks.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the the indexOf method for String. You'll use something like this
to find the space seperating the first and last name. And then use that index for your substring calls. Of course this assumes you only have a first and last name and not a middle name as well. If there is a middle name you'll need to find the index for both spaces.

[ April 12, 2006: Message edited by: Hentay Duke ]
[ April 12, 2006: Message edited by: Hentay Duke ]
 
Santhini Nesaraj
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. It works.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a StringTokenizer to seperate the words. Or
another more cumbersome way, (but simpler in terms of helping you learn) would be to get the ASCII value for each character, when the value is equivalent to that of a space,32, then divide the string into substrings from that char as a central pivot. Then you can do whatever you like with them - for instance put them into an array of first names and an array of second names. Then for each entry e.g. in your firstNamesArray[5] will correspond to LastNmaesArray[5] and you can concat. these two values to get the full name.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can use a StringTokenizer to seperate the words...


Note that according to the documentation, StringTokenizer is a legacy class and its use in new code is discouraged.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im not really sure, but i think white space is considered a character of the string "john doe"

so you could easily use substring of (4,7) and i believe that would be doe.

then do hmmm not sure how you would get the john, seeing how the first parameter of the substring method is ignored....

lol owell i tried...

justin
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for small strings such as this, you can use the String.split(regex) method which will return an array of strings that are split around the matches.

e.g.

[ April 16, 2006: Message edited by: Kristian Perkins ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys, i saw there are lot of option are available for your Question . i belive all are correct, but to get the Best one by implementing Regular Expression in the Java Code which has been add in the java 1.5 .. if u have any other query Reply . java2usuf@gmail.com
 
Kristian Perkins
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


...Regular Expression in the Java Code which has been add in the java 1.5...


actually regular expressions were added in 1.4, so if your program will be running on a minimum of java 1.4 you will be able to use them (String.split() and java.util.regex package).
 
reply
    Bookmark Topic Watch Topic
  • New Topic