ilsoviet1917 发表于 2022-4-25 17:14:53

中英文数字符号混编的字符串问题

把中英文数字符号混编的字符串按照不同类型截取,比如“这是我的iPhone13,那是你的Meta30。”
截取成:
“这是我的”
“iPhone”
“13”
“,”
“那是你的”
“Meta”
“30”
“。”
然后再按顺序把这八个字符串存入到一个集合。

我不知道这种想法是否能够实现,判断字符类型是用正则表达式方便呢?还是逐个字符判断方便?

我自己写了一下
ArrayList al = new ArrayList();
            StringBuilder sb = new StringBuilder
                ("这是我的iPhone 13, 这个是你的Mate 30。");
            StringBuilder resultOfSplit = new StringBuilder();


            for (int i = 0; i < sb.Length; i++)
            {
                if (sb >= 0x4e00 && sb <= 0x9fbb)
                {
                  resultOfSplit.Append(sb);
                }
                else
                {
                  sb.Remove(0, i);
                  break;
                }
                al.Add(resultOfSplit);
            }

这样只能提取到”这是我的“几个字,但是在sb删除这四个字以后,不知道怎么重新开始继续提取之后的字符串了。

basilizak 发表于 2022-9-7 15:31:13

写了个简单的,你自己优化吧。
            string str = "这是我的iPhone 13, 这个是你的Mate 30。";
            int last = 0;
            for(int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str))
                {
                  if (str >= '一')
                  {
                        if (last != 1)
                        {
                            last = 1;
                            Console.WriteLine();
                        }
                        Console.Write(str);
                  }
                  else
                  {
                        if (last != 2)
                        {
                            last = 2;
                            Console.WriteLine();
                        }
                        Console.Write(str);
                  }
                }
                if (char.IsDigit(str))
                {
                  if (last != 3)
                  {
                        last = 3;
                        Console.WriteLine();
                  }
                  Console.Write(str);
                }
                if (char.IsPunctuation(str))
                {
                  if (last != 4)
                  {
                        last = 4;
                        Console.WriteLine();
                  }
                  Console.Write(str);
                }

            }
            Console.ReadLine();
页: [1]
查看完整版本: 中英文数字符号混编的字符串问题