如何在c#中拆分文本? [英] How to split text in c#?
本文介绍了如何在c#中拆分文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我是c#中的新手,我在拆分文本方面遇到了麻烦。
i有一个例子:
字符串:STR_LOCAL_ABS_CERAMIUM_A21
i希望在STR_LOCAL_ABS_之间获取文本CERAMIUM和_A21
你能帮帮我吗?
谢谢
Hi everybody!
Im new in c# and i have a trouble with split text.
i have a example:
String: STR_LOCAL_ABS_CERAMIUM_A21
i want get text "CERAMIUM" between "STR_LOCAL_ABS_" and "_A21"
can you help me?
thanks
推荐答案
有很多方法可以做到这一点,最简单的可能是使用拆分:
There are quite a few ways to do that, the simplest is probably to use Split:
string inp = "STR_LOCAL_ABS_CERAMIUM_A21";
string[] parts = inp.Split('_');
parts [3]
将有CERAMIUM - 如果你想要的位总是在同一药水中,这样就可以了。
如果它总是在相对于末尾的相同位置,那么使用部分[parts.Length - 2]
而不是。
parts[3]
will have "CERAMIUM" - if the "bit" you want is always in the same potion, that will do it.
If it's always in the same position relative to the end, then use parts[parts.Length - 2]
instead.
以下代码可能对您有所帮助。
The following code could be helpful for you.
using System;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
string strText = "STR_LOCAL_ABS_CERAMIUM_A21";
string[] container = strText.Split('_');
//First way
for (int i = 0; i < container.Length; i++)
{
if (i == (container.Length-2))
{
Console.WriteLine("value is _{0}", container[i]);
}
}
//Second Way
foreach (string item in container)
{
if (item.Contains("CERAMIUM"))
{
Console.WriteLine("value is _{0}", item);
}
}
Console.ReadKey();
}
}
}
这篇关于如何在c#中拆分文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文