File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Substring" Watch "Substring" New topic
Author

Substring

Santhini Nesaraj
Greenhorn

Joined: May 16, 2005
Posts: 9
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.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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

Joined: May 16, 2005
Posts: 9
Thanks a lot. It works.
Eoin Mac Aoidh
Ranch Hand

Joined: May 19, 2005
Posts: 48
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.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Kristian Perkins
Ranch Hand

Joined: Mar 27, 2006
Posts: 32
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 ]
Mohammed Yousuff
Ranch Hand

Joined: Apr 18, 2006
Posts: 45
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


Regards<br /> <br />Mohammed Yousuff M N <br /> <br />Try NOT to Become a man of SUCCESS, BUT Try to Become a man of VALUE..
Kristian Perkins
Ranch Hand

Joined: Mar 27, 2006
Posts: 32

...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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Substring