简体   繁体   English

需要从C#中的字符串中提取字段

[英]Need to extract fields from a string in C#

I have extract the 3 usable field from a string. 我从字符串中提取了3个可用字段。 There is no common delimiter, there can be both blank spaces and tabs. 没有通用的分隔符,可以有空格和制表符。

First, what I am doing is replacing all double blanks and tabs by '**' 首先,我正在做的是用'**'替换所有双重空格和标签

Given String : 给定字符串:

cont = Gallipelle 04/04/2012 16.03.03 5678 cont = Gallipelle 04/04/2012 16.03.03 5678

I am using: 我在用:

cont.Replace(" ", "**").Replace(" ", "**").Replace(" ", "**").Replace("**", "").Trim() cont.Replace(“”,“**”)。替换(“”,“**”)。替换(“”,“**”)。替换(“**”,“”).Trim()

The answer becomes: 答案变成:

****** Gallipelle******04/04/2012 16.03.03************************ 5678***** ****** Gallipelle ****** 04/04/2012 16.03.03 ************************ 5678 **** *

Is the approach correct? 方法是否正确? How do I extract the stuffs from here? 如何从这里提取东西? I just need all the extracts in string datatype. 我只需要字符串数据类型中的所有提取。

Just use String.Split : 只需使用String.Split

var fields = cont.Split(new[] { " ", "\t" },
                        StringSplitOptions.RemoveEmptyEntries);

Adding StringSplitOptions.RemoveEmptyEntries makes sure that if there are multiple consecutive tabs and/or spaces they will "count as one" when extracting the results. 添加StringSplitOptions.RemoveEmptyEntries可确保在提取结果时,如果有多个连续的选项卡和/或空格,它们将“计为一个”。

An alternate option would be to use a regular expression. 另一种选择是使用正则表达式。

You can use regex groups to find out three values name, date, number. 您可以使用正则表达式组来查找三个值名称,日期,数字。

A group is defined as (?<group_name><regex_expr>) 组被定义为(?<group_name> <regex_expr>)

So you could write 所以你可以写

            Regex regex = new Regex("(?<name>(\\S*))(\\s*)(?<date>((\\S*)\\s(\\S*)))(\\s*)(?<number>(\\d*))");
            Match match = regex.Match(yourString);
            if (match.Success)
            {
                string name = match.Groups["name"].Value;
                string date = match.Groups["date"].Value;
                string number = match.Groups["number"].Value;
            }

\\s* matches sequence of whitespaces which includes tabs. \\ s *匹配包含制表符的空格序列。 \\S* matches sequence of non-whitespace characters. \\ S *匹配非空白字符的序列。 \\d* matches sequence of digits. \\ d *匹配数字序列。

(new Regex("\\s+")).Split(yourstring)

http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx

var myText="cont = Gallipelle 04/04/2012 16.03.03 5678";
var splitString=myText.split(" ");

// splitString[1] == Gallipelle 
// splitString[2] ==  04/04/2012
// splitString[3] == 16.03.03 
// splitString[4] == 5678

No. No need to replace it with any other delimiter. 不需要。不需要用任何其他分隔符替换它。 You can use String's split function and give 'space' as delimiter character. 您可以使用String的split函数并将'space'作为分隔符。 eg in VB.Net: 例如在VB.Net中:

Dim value As String() = cont.split(CChar(" ")) 昏暗值As String()= cont.split(CChar(“”))

this will give you a string array whose values you can access: value(0), value(1) and value(2) 这将为您提供一个字符串数组,您可以访问其值:value(0),value(1)和value(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM