PDA

View Full Version : [Java] Controllare se una stringa contiene un'altra stringa


e-commerce84
02-06-2009, 11:59
Ciao,
se ho 2 stringhe...come posso controllare se una contiene l'altra?

Grazie
Andrea

ndakota
02-06-2009, 12:44
non ne sono certo, forse questo metodo della classe String potrebbe fare al caso tuo.


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

carter100
02-06-2009, 19:46
non ne sono certo, forse questo metodo della classe String potrebbe fare al caso tuo.
cut


E' corretto, un altro metodo sarebbe il contains, sempre della classe String.