dsfextreme 30 de abr. de 2006
paribe:
Olá pessoal ,
tenho uma duvida referente ao split no exemplo abaixo
o que ele está fazendo ?
E no for como posso fazer pra ver o conteudo de array sa
pois usando sa[x] da erro ?
public class StringSplit {
public static void main ( String [] args ) {
String s = "x1234 y56 z7 a" ;
String [] sa = s . split ( "\d" );
int count = 0 ;
for ( String x : sa ) {
count ++ ;
}
System . out . println ( "total: " + count );
}
}
Cara meu inglês não é bom mas isso que a classe String com o método split faz´.Pelo que eu entedí ele forma uma expressão regular que testa seus caracteres conforme o que vc incluiu.
/**
* Splits this string around matches of the given
* {@linkplain java.util.regex.Pattern#sum regular expression}.
*
* <p> This method works as if by invoking the two-argument {@link
* #split(String, int) split} method with the given expression and a limit
* argument of zero. Trailing empty strings are therefore not included in
* the resulting array.
*
* <p> The string <tt> "boo:and:foo"</tt> , for example, yields the following
* results with these expressions:
*
* <blockquote><table cellpadding= 1 cellspacing= 0 summary= "Split examples showing regex and result" >
* <tr>
* <th> Regex</th>
* <th> Result</th>
* </tr>
* <tr><td align= center > :</td>
* <td><tt> { "boo", "and", "foo" }</tt></td></tr>
* <tr><td align= center > o</td>
* <td><tt> { "b", "", ":and:f" }</tt></td></tr>
* </table></blockquote>
*
*
* @param regex
* the delimiting regular expression
*
* @return the array of strings computed by splitting this string
* around matches of the given regular expression
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public String[] split(String regex) {
return split(regex, 0);
}
Já o conteúdo da sua String sa esta em anexo,um abraço !