级联字符串拆分 [英] concatenated string spliting

查看:52
本文介绍了级联字符串拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

农业(粮食,粮食,小麦)AgPro Grain2"

我需要将字符串分成三部分.

1)农业(食品,粮食,小麦库)
2)AgPro Grain2
3)2

我尝试了以下方法,但没有任何方法分开我想要的方式.

"AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)AgPro Grain2"

I need to split that string in to 3 parts.

1) AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)
2) AgPro Grain2
3) 2

I tried the following but nothing split the way I want.

char seperator      = '_';

char[] seperator = new Char[] { '.' };
char[] seperator = new Char[] {'.'};

char[] seperator = new Char[] { ',', ' ' };
char[] seperator      = new char[]{};
char[] seperator      = new char[]{};
char seperator      = {};



可以按照我想要的方式拆分并返回数组的正确语法是什么?



What is the right syntax that can split it the way I want and return in the array?

推荐答案

我将使用正则表达式:这可能无法正常工作,这取决于根据您的标准(未指定):
I would use a regex: This may not work exactly, it depends on your criteria, which you don''t specify:
^(?<description>[^)]*\))(?<shortname>[^\d]*)(?<count>\d+)


</count></shortname></description>
</count></shortname></description>

将其分为三组.


您的字符串没有一个可识别的模式,这意味着您将需要手工进行操作. String.Split只能有所帮助.
Your string doesn''t have an identifiable pattern, so that means you''re going to have to do it by hand. String.Split can only help a little.
string  str = "AGRICULTURE(FOOD,GRAIN,WHEAT POOLS)AgPro Grain2";
string[] temp =  str.Split(')');
temp[0] += ")";
string temp2 = str.Substring(str.Length - 1);
string[3] result = new string("","","");
Array.Copy(temp, 0, result, 0, temp.Length);
result[2] = temp2;


那应该给你你想要的(可能需要调整,因为我没有测试它.).


That should give you what you want (may need to be tweaked because I didn''t test it).


这篇关于级联字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆