Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting first characters from string...

 
Ilja Smoli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja
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
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General( beginner )
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Hope this will help.
[code]
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.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
[code]
 
Ilja Smoli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx a lot..
I have to read specifications carefully
didn't noticed for substring function..

sry for disturbing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic