鱼C论坛

 找回密码
 立即注册
查看: 1300|回复: 1

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

[复制链接]
发表于 2022-4-25 17:14:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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


  5.             for (int i = 0; i < sb.Length; i++)
  6.             {
  7.                 if (sb[i] >= 0x4e00 && sb[i] <= 0x9fbb)
  8.                 {
  9.                     resultOfSplit.Append(sb[i]);
  10.                 }
  11.                 else
  12.                 {
  13.                     sb.Remove(0, i);
  14.                     break;
  15.                 }
  16.                 al.Add(resultOfSplit);
  17.             }
复制代码


这样只能提取到”这是我的“几个字,但是在sb删除这四个字以后,不知道怎么重新开始继续提取之后的字符串了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-7 15:31:13 | 显示全部楼层
写了个简单的,你自己优化吧。
  1.             string str = "这是我的iPhone 13, 这个是你的Mate 30。";
  2.             int last = 0;
  3.             for(int i = 0; i < str.Length; i++)
  4.             {
  5.                 if (char.IsLetter(str[i]))
  6.                 {
  7.                     if (str[i] >= '一')
  8.                     {
  9.                         if (last != 1)
  10.                         {
  11.                             last = 1;
  12.                             Console.WriteLine();
  13.                         }
  14.                         Console.Write(str[i]);
  15.                     }
  16.                     else
  17.                     {
  18.                         if (last != 2)
  19.                         {
  20.                             last = 2;
  21.                             Console.WriteLine();
  22.                         }
  23.                         Console.Write(str[i]);
  24.                     }
  25.                 }
  26.                 if (char.IsDigit(str[i]))
  27.                 {
  28.                     if (last != 3)
  29.                     {
  30.                         last = 3;
  31.                         Console.WriteLine();
  32.                     }
  33.                     Console.Write(str[i]);
  34.                 }
  35.                 if (char.IsPunctuation(str[i]))
  36.                 {
  37.                     if (last != 4)
  38.                     {
  39.                         last = 4;
  40.                         Console.WriteLine();
  41.                     }
  42.                     Console.Write(str[i]);
  43.                 }

  44.             }
  45.             Console.ReadLine();
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表