Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instr function?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a string like:

*test*te*12*2*

I'd like to come up with an array like:

Arr[1] = test
Arr[2] = te
Arr[3] = 12
Arr[4] = 2

On VB this would be simple, what's the best way of doing it on Java? Some sample code would be nice.

Thank you
 
use the string tokeniser and specify that * should be your delimiter.
bruce21.jpg
 
Actually your post title and post content are two different questions. The title is asking for a Java equivalent to InStr.

Here is Instr in VB:
Code:
InStr Function
Returns the position of the first occurrence of one string within another.

The Java equivalent is the indexof method in the String class. Here is the Javadocs:
Code:
indexOf

public int indexOf(String str)


Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:


 this.startsWith(str, k)
 
is true.

Parameters:

str - any string.

Returns:

if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

Throws:

NullPointerException - if str is null.
Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top