string animals = "cat98dog75";
什么,我尽量做到:
字符串= “cat98”;
串B = “dog75”;
问:
我怎么分割使用一些范围分隔字符串?
例如:
animals.split();
string animals = "cat98dog75";
什么,我尽量做到:
字符串= “cat98”;
串B = “dog75”;
问:
我怎么分割使用一些范围分隔字符串?
例如:
animals.split();
我建议匹配与正则表达式帮助:
using System.Text.RegularExpressions;
...
string animals = "cat98dog75";
string[] items = Regex
.Matches(animals, "[a-zA-Z]+[0-9]*")
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
string a = items[0];
string b = items[1];
Concole.Write(string.Join(", ", items));
结果:
cat98, dog75
如果你想分裂初始字符串b Ÿ相等大小的块:
int size = 5;
string[] items = Enumerable
.Range(0, animals.Length/size + (animals.Length % size > 0 ? 1 : 0))
.Select(index => (index + 1) * size <= animals.Length
? animals.Substring(index * size, size)
: animals.Substring(index * size))
.ToArray();
string a = items[0];
string b = items[1];
这就是即时通讯由大小相等的块找字符串。 – Edulo
@Edulo:下一次,请,更具体:“拆分到字符串数组没有分隔符”可以在许多不同的方式阅读。 –
如果要拆分的动物和数量的名称,请尝试以下这可能会为你
string animals = "cat98dog75";
string[] DiffAnimals = Regex.Split(animals, "(?<=[0-9]{2})")
.Where(s => s != String.Empty) //Just to Remove Empty Entries.
.ToArray();
做的伎俩..
我知道它太久......
private static void SplitChars()
{
string animals = "cat98dog75";
Dictionary<string, string> dMyobject = new Dictionary<string, string>();
string sType = "",sCount = "";
bool bLastOneWasNum = false;
foreach (var item in animals.ToCharArray())
{
if (char.IsLetter(item))
{
if (bLastOneWasNum)
{
dMyobject.Add(sType, sCount);
sType = ""; sCount = "";
bLastOneWasNum = false;
}
sType = sType + item;
}
else if (char.IsNumber(item))
{
bLastOneWasNum = true;
sCount = sCount + item;
}
}
dMyobject.Add(sType, sCount);
foreach (var item in dMyobject)
{
Console.WriteLine(item.Key + "- " + item.Value);
}
}
你会得到输出为
猫 - 98
狗 - 75
基本上,你得到的类型和数量,所以如果你想使用的计数,你不需要再次分裂......
什么是你的分隔符? –
您可以使用子。 –
[字符串分割为具有一定规模的大块]的可能的复制(http://stackoverflow.com/questions/1450774/splitting-a-string-into-chunks-of-a-certain-size) – Reniuz