字符串拆分成数组,如,“ab&&2”,通过“&&”做分隔符,分割得到字符串数组[ab,2]

1.接口


   
   
  1. public interface NoSplitInter {
  2. public abstract void nosplit ();
  3. }

2.实现类


   
   
  1. public class NoSplitImpl implements NoSplitInter {
  2. private String str;
  3. ArrayList<String> list = new ArrayList<String>();
  4. public NoSplitImpl (String str) {
  5. this.str = str;
  6. }
  7. @Override
  8. public void nosplit () {
  9. //字符串如果不为空
  10. while (!str.isEmpty()) {
  11. //如果字符串中包含 剔除的字符串
  12. if (str.contains( "&&")) {
  13. //存在 获得该字符串的下标 截取从0到下标的字符串
  14. int index = str.indexOf( "&&");
  15. list.add(str.substring( 0, index));
  16. // 然后将刚刚已经添加到集合中的部分通过切割舍去
  17. str = str.substring(index + 2);
  18. } else {
  19. //不存在 就把最后一部分添加进集合
  20. list.add(str);
  21. break;
  22. }
  23. }
  24. System.out.println(list);
  25. }
  26. }

3.main方法


   
   
  1. public class NOSplit {
  2. public static void main (String[] args) {
  3. // 多个不同的符号可以使用这个方法 \\w*\\w匹配字母数字
  4. // String regex = "\\w*\\w";
  5. // Pattern pattern = Pattern.compile(regex);
  6. // Matcher matcher = pattern.matcher(str);
  7. //
  8. // while (matcher.find()) {
  9. // System.out.print(matcher.group());
  10. // }
  11. String str = "ab&&c";
  12. NoSplitImpl noSplit = new NoSplitImpl(str);
  13. noSplit.nosplit();
  14. }
  15. }

字符串组合

实现字符串组合,如:[“ab”,“2”]通过“&&”分隔符,组合成字符串“ab&&2
   
   

 1.接口


   
   
  1. public interface StrAddInter {
  2. public abstract void strAdd ();
  3. }

2.实现类


   
   
  1. public class StrAddImpl implements StrAddInter {
  2. private String[] str;
  3. //用来放合并后的字符串
  4. private String newStr = "";
  5. public StrAddImpl (String[] str) {
  6. this.str = str;
  7. }
  8. @Override
  9. public void strAdd () {
  10. for (String s : str) {
  11. //输出为ab&&2&&
  12. newStr += s.substring( 0, s.length()) + "&&";
  13. }
  14. //需要截取后面的&&
  15. System.out.println(newStr.substring( 0,newStr.length()- 2));
  16. }
  17. }

3.main方法


   
   
  1. public class NOSplit {
  2. public static void main (String[] args) {
  3. String str = "ab&&c";
  4. String[] strArrs = { "ab", "2"};
  5. System.out.println(Arrays.toString(strArrs));
  6. StrAddImpl strAdd = new StrAddImpl(strArrs);
  7. strAdd.strAdd();
  8. }

   
   
  1. public class NOSplit {
  2. public static void main (String[] args) {
  3. String str = "ab&&c";
  4. String[] strArrs = { "ab", "2"};
  5. // 多个不同的符号可以使用这个方法 \\w*\\w匹配字母数字
  6. // String regex = "\\w*\\w";
  7. // Pattern pattern = Pattern.compile(regex);
  8. // Matcher matcher = pattern.matcher(str);
  9. //
  10. // while (matcher.find()) {
  11. // System.out.print(matcher.group());
  12. // }
  13. NoSplitImpl noSplit = new NoSplitImpl(str);
  14. noSplit.nosplit();
  15. System.out.println( "--------------");
  16. System.out.println(Arrays.toString(strArrs));
  17. StrAddImpl strAdd = new StrAddImpl(strArrs);
  18. strAdd.strAdd();
  19. System.out.println( "---------------");
  20. char[] chars = str.toCharArray();
  21. ArrayList<String> list = new ArrayList<>();
  22. for ( char c : chars) {
  23. System.out.println(c);
  24. if (c != '&') {
  25. list.add(String.valueOf(c));
  26. }
  27. }
  28. System.out.println(list);
  29. }
  30. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值