split 分割中文字的问题

zzmdegm 2010-10-18 11:13:50
有一字符串:string _expressionDIY = "百位*3-十位+2+个位*个位";
想把它分割成这样的字符串数组:“百位”,“*3-”,“十位”,“+2+”,“个位”,“*”,“个位”

我的做法:
string[] _a = new string[] {"百位","十位","个位" };
string[] _b = _expressionDIY.Split(_a, System.StringSplitOptions.None);

但结果却没有"百位","十位","个位"了,怎么能原样的保存它们呢?
...全文
557 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzmdegm 2010-10-19
  • 打赏
  • 举报
回复
啊!好麻烦,算了,用字符串替换来做吧!
_expressionDIY.Replace
谢谢大家了!
wuyq11 2010-10-18
  • 打赏
  • 举报
回复
string str= "百位*3-十位+2+个位*个位";
string[] arr=str.Split(new string[]{"百位","十位","个位"},StringSplitOptions.RemoveEmptyEntries);
Buffer.BlockCopy方法是将一个数组的字节——不是元素——复制到另一个数组中去
public string[] CompoundArray(params int[][] arrays)
{
List<string> list = new List<string>();
foreach (int[] arr in arrays)
list.AddRange(arr);
return list.ToArray();
}
ckp00001 2010-10-18
  • 打赏
  • 举报
回复

string _expressionDIY = "百位*3-十位+2+个位*个位";
string[] _a = new string[] { "百位", "十位", "个位" };
for( int i = 0; i < _a.Length ;i++)
{_expressionDIY= _expressionDIY.Replace(_a[i], "," + _a[i] + ",");}

_b = _expressionDIY.Split(",".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries );
龍月 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 zzmdegm 的回复:]
有一字符串:string _expressionDIY = "百位*3-十位+2+个位*个位";
想把它分割成这样的字符串数组:“百位”,“*3-”,“十位”,“+2+”,“个位”,“*”,“个位”

我的做法:
string[] _a = new string[] {"百位","十位","个位" };
string[] _b = _expressionDIY.Split(_a, Sys……
[/Quote]
分成了 两个数组
合成就行了啊
sprc_lcl 2010-10-18
  • 打赏
  • 举报
回复
            string _expressionDIY = "百位*3-十位+2+个位*个位";
string[] strP = new string[]{"百位","十位","个位"};
List<string> listStr = new List<string>();//结果
while(_expressionDIY.Length>0)
{
bool flag = false;
int index = 0;
for(int i=0;i<strP.Length;i++)
{
int _index = _expressionDIY.IndexOf(strP[i]);
if(_index == 0)
{
flag = true;
listStr.Add(strP[i]);
_expressionDIY = _expressionDIY.Substring(strP[i].Length);
break;
}
if (_index == -1) continue;
if(index == 0 || _index<index) index = _index;
}
if(flag) continue;
if (index == 0) index = _expressionDIY.Length;
listStr.Add(_expressionDIY.Substring(0,index));
_expressionDIY = _expressionDIY.Substring(index);
}

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧