jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes getting first characters from string... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting first characters from string..." Watch "getting first characters from string..." New topic

getting first characters from string...

Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Quick newbie question
I have a string:
String a = "this is a string";
and i need to get for e.g first 3 characters from it, that output were "thi"..
Is any easy way to do it? coz I done this using loop with charAt() function...
and also I need to get some characters from middle of string...
In VBScript there is functions LEFT() and MID() for performing these operations...
How about java? I didn't found it in jsp specification..
Thx in advance..
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
What you are looking for is the substring method of the string class. It takes two ints as arguments. The first is the starting index and the second is one greater than the ending index.
You can check the API for all the details.
Also, this is the JSP forum and your post is not about JSPs. Please try to post questions to the correct forums and you will get better responses.

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Moving to Java in General( beginner )

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
aminur rashid
Ranch Hand

Joined: Sep 21, 2001
Posts: 79
Hi ,
Hope this will help.
public String substring(int beginIndex,
int endIndex)Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Regards<br />Aminur
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Thx a lot..
I have to read specifications carefully
didn't noticed for substring function..

sry for disturbing
I agree. Here's the link: http://aspose.com/file-tools
subject: getting first characters from string...
It's not a secret anymore!