Splitting a String is the most day to day required activity for a programmer. You must have a good grasp of the process of splitting a String.
We have two widely used possible ways of splitting a String in java.
- split(), &
- StringTokenizer()
String : Learning|Java|Is|Fun
Delimmiter: |
public class SplitString {
public static void main(String[] args) {
String s1 = "Learning|Java|Is|Fun";
//String[] s2 = s1.split("\\|");
String[] s2 = s1.split(Pattern.quote("|")); //you need to importjava.util.regex.Pattern
int i = 0;
while (i < s2.length) {
System.out.println(s2[i]);
i++;
}
}
No comments:
Post a Comment