on 06-27-2007 10:12 AM
does anyone know, i got an error when i try to use method split and replaceAll,
eg. String a ="123";
if i try a.split or a.replaceAll then it will be error. i think split or replaceAll is standard method for string?
This depends on which Java version you have, see e.g.
/**
* 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);
}
Armin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
An Example Program to describe the wprking of split and replaceAll
public class test {
public static void main(String args[])
{
String a="123";
String a1[]=a.split("2");
String a2=a.replaceAll("1","5");
for(int i=0;i<a1.length;i++)
System.out.println(a1<i>);
System.out.println(a2);
}
}
Output is
1
3
523
(123 is plitted into 1 and 2)
(123 is replaced as 523)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hai,
Split, replace all methods are inbuit with string class.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Can you paste your code here?
Regards,
Naga
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Oscar,
String has certain in built methods such as
split(),
substring(),
subSequence()
replace()
replaceAll()
replaceFirst()
Split will split the given string in to <b>string arrays</b> based on the condition.
replace all will replace the <b>string</b> with given string.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.