English 中文(简体)
C#中扼杀的数值
原标题:Extracting values from a string in C#
  • 时间:2011-09-23 09:20:22
  •  标签:
  • c#

我有以下阐述,希望从以下几方面收回一些价值观:

============================
Control 127232:
map #;-
============================
Control 127235:
map $;NULL
============================
Control 127236:

我只想控制。 因此,从以上所述中可以检索到包含<代码>的阵列[127232,127235,127236]?

最佳回答

try this (assuming your string is named s and each line is made with ):

List<string> ret = new List<string>();
foreach (string t in s.Split( 
 ).Where(p => p.StartsWith("Control")))
    ret.Add(t.Replace("Control ", "").Replace(":", ""));

ret.Add.(......) part is not elegant, but work...

EDITED:
If you want an array use string[] arr = ret.ToArray();

SYNOPSYS:
I see you re really a newbie, so I try to explain:

  • s.Split( ) creates a string[] (every line in your string)
  • .Where(...) part extracts from the array only strings starting with Control
  • foreach part navigates through returned array taking one string at a time
  • t.Replace(..) cuts unwanted string out
  • ret.Add(...) finally adds searched items into returning list
问题回答

实现这一点的一个途径是定期表达,这确实带来了一些复杂性,但会给你以某种良好的衡量标准。

在一组人中,定期表明你想要的数据:

var regex = new Regex(@"Controls+(d+):");

这将寻找字面上描述的“Control”,随后是一个或多个白色空间特征,随后是一个或多个数字(在一组捕获组内),随后是字面上的“:”。

接着,利用上述定期表述,从你的意见中获取:

var matches = regex.Matches(inputString);

然后,使用LINQ的比照,你可以把这变成一个阵列。

var arr = matches.OfType<Match>()
                 .Select(m => long.Parse(m.Groups[1].Value))
                 .ToArray();

如今,<代码>arr是一阵列,仅载有编号。

现场实例:

Off the top of my head try this (it s quick and dirty), assuming the text you want to search is in the variable text :

        List<string> numbers = System.Text.RegularExpressions.Regex.Split(text, "[^\d+]").ToList();
        numbers.RemoveAll(item => item == "");   

第1行将所有数字分成一个单独项目,还把大量空洞分开,第2行删除了空洞,把三人名单交给了你。 如果你想把这一回头换成一个阵列,那么最终就添加以下线:

        var numberArray = numbers.ToArray();

是的,有办法。 我可以回顾一下它的一个简单方式,但为了提取这种价值,必须加以限定。 下面是:

  1. Find a word "Control" in string and its end
  2. Find a group of digits after the word
  3. Extract number by int.parse or TryParse
  4. If not the end of the string - goto to step one

实现这一算法几乎是原始的。

This is simplest implementation (your string is str):

    int i, number, index = 0;        
    while ((index = str.IndexOf( : , index)) != -1)
    {
        i = index - 1;
        while (i >= 0 && char.IsDigit(str[i])) i--;
        if (++i < index)
        {
            number = int.Parse(str.Substring(i, index - i));
            Console.WriteLine("Number: " + number);
        }
        index ++;
    }

对这种很少的业务使用准则是令人怀疑的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签