Java String Split Example

*

2.
Java String split example.

3.
This Java String split example describes how Java String is split into multiple

4.
Java String objects.

5.
*/

6.
 

7.
public class JavaStringSplitExample{

8.
 

9.
  public static void main(String args[]){

10.
  /*

11.
  Java String class defines following methods to split Java String object.

12.
  String[] split( String regularExpression )

13.
  Splits the string according to given regular expression.

14.
  String[] split( String reularExpression, int limit )

15.
  Splits the string according to given regular expression. The number of resultant 

16.
  substrings by splitting the string is controlled by limit argument.

17.
  */

18.
  

19.
  /* String to split. */

20.
  String str = "one-two-three";

21.
  String[] temp;

22.
  

23.
  /* delimiter */

24.
  String delimiter = "-";

25.
  /* given string will be split by the argument delimiter provided. */

26.
  temp = str.split(delimiter);

27.
  /* print substrings */

28.
  for(int i =0; i < temp.length ; i++)

29.
    System.out.println(temp[i]);

30.
  

31.
  /*

32.
  IMPORTANT : Some special characters need to be escaped while providing them as 

33.
  delimiters like "." and "|".

34.
  */

35.
  

36.
  System.out.println("");

37.
  str = "one.two.three";

38.
  delimiter = "\\.";

39.
  temp = str.split(delimiter);

40.
  for(int i =0; i < temp.length ; i++)

41.
    System.out.println(temp[i]);

42.
  

43.
  /*

44.
  Using second argument in the String.split() method, we can control the maximum 

45.
  number of substrings generated by splitting a string.

46.
  */

47.
  

48.
  System.out.println("");

49.
  temp = str.split(delimiter,2);

50.
  for(int i =0; i < temp.length ; i++)

51.
    System.out.println(temp[i]);

52.
  

53.
  }

54.
 

55.
}

56.
 

57.
/*

58.
OUTPUT of the above given Java String split Example would be :

59.
one

60.
two

61.
three

62.
one

63.
two

64.
three

65.
one

66.
two.three

67.
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值