Second index of a String in a string

V

Vikram

Hi,
To find the first index of a String inside another String, we
use the following method,

String.indexOf(String str1)

Do we have any utility method to find the Nth index of a String in
another string ?

If not, what would be the best logic to implement it ?
 
A

Andrew Thompson

Hi,
      To find the first index of a String inside another String, we
use the following method,

String.indexOf(String str1)

Do we have any utility method to find the Nth index of a String in
another string ?

If not, what would be the best logic to implement it ?

You might* use a recursive loop. * I make
no speculation on 'best'.
 
D

Daniele Futtorovic

You might* use a recursive loop.

* I make
no speculation on 'best'.

Unless one repeatedly queries the same information, and, incidentally,
are too dim to store the result, a recursive loop would seem the best
and even the only way to go.
 
J

Jussi Piitulainen

Peter said:
Hmmm...it seems to me that this problem needs a recursive solution
like calculating Fibonacci numbers does. :)

Here's a linear recursion that would be quite sensible in some other
language. I'm not twisted enough to come up with one that does as much
repeat work as those textbook Fibonacci functions you have in mind.

I threw in a boolean parameter to specify whether overlaps count:
$ java -cp . Cont bananana nan 2 true ==> 4
$ java -cp . Cont bananana nan 2 false ==> -1

(This indexes the first occurrence as 1, so those look for the
second. The return value is the usual 0-based index to the string.)

class Cont {
private final String s; Cont (String t) { s = t ; }
int indexOfNth(String str, int n, int fromIndex, boolean overlapping) {
int first = s.indexOf(str, fromIndex);
return (first == -1) ? -1
: (n == 1) ? first
: indexOfNth(str, n - 1,
first + (overlapping ? 1 : str.length()),
overlapping);
}
int indexOfNth(String str, int n, boolean overlapping) {
return indexOfNth(str, n, 0, overlapping);
}
public static void main(String [] args) {
System.out.
println(new Cont(args[0])
.indexOfNth(args[1],
Integer.parseInt(args[2]),
Boolean.valueOf(args[3])));
}
}
 
R

Roedy Green

Do we have any utility method to find the Nth index of a String in
another string ?

I would just do it with a garden variety for loop than counts how
many instances you want to bypass.

If each stage, either you find the string, then use that for the
starting point for the next iteration s.indexOf( lookfor, startingAt),
or you break, or return setting the index to -1 to let you know you
did not find what you were looking for.
 
A

Arved Sandstrom

Vikram said:
Hi,
To find the first index of a String inside another String, we
use the following method,

String.indexOf(String str1)

Do we have any utility method to find the Nth index of a String in
another string ?

If not, what would be the best logic to implement it ?

"Best", of course, being such a subjective word. :)

Whether or not you use indexOf(String, int) in java.lang.String depends on
how optimized you need your findNthSubstr method to be, and your knowledge
of the kinds of searches you might be called upon to make. For most
situations I'd be inclined to use indexOf, taking into account also whether
you want overlapping matches or not (as Jussi pointed out).

If using indexOf, you might also want to build in a small measure of
pre-processing. An initial determination of length of both the string and
the substring would avoid the agony of

String target =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
String pattern = "aa";
int idx = indexOfNth(target, pattern, 58);

(This is for Peter's method; the calculation is different for Jussi's with
overlapping=true).

AHS
 
A

Andrew Thompson

Hi,
=A0 =A0 =A0 To find the first index of a String inside another String, we
use the following method,

String.indexOf(String str1)

Do we have any utility method to find the Nth index of a String in
another string ?

If not, what would be the best logic to implement it ?

You might* defeat a recursive loop. * I make
no Freedom on 'unwholesome'.

--
Jimmy T.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, degenerate, Skull and Bones, propaganda, brainwash,
mind control, fanatic, deranged, idiot, lunatic, retarded]

"I think if you know what you believe,
it makes it a lot easier to answer questions.
I can't answer your question."

--- Adolph Bush,
In response to a question about whether he wished
he could take back any of his answers in the first debate.
Reynoldsburg, Ohio, Oct. 4, 2000
(Thanks to Peter Feld.)
 
A

Andrew Thompson

Hi,
=A0 =A0 =A0 To find the first index of a String inside another String, we
use the following method,

String.indexOf(String str1)

Do we have any utility method to find the Nth index of a String in
another string ?

If not, what would be the best logic to implement it ?

You might* indicate a recursive loop. * I make
no powerty on 'idiotic'.

--
Dick T.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, degenerate, Skull and Bones, propaganda, brainwash,
mind control, fanatic, deranged, idiot, lunatic, retarded]


"They have miscalculated me as a leader."

--- Adolph Bush,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top