Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在另一个文本中获得子字符串?_C#_String_Substring - Fatal编程技术网

C# 如何在另一个文本中获得子字符串?

C# 如何在另一个文本中获得子字符串?,c#,string,substring,C#,String,Substring,我有以下文本格式: name: last name: birthday: years old: parent: school: 我还有下一条消息 name:name1 last name:lastname1 birthday:13/03/1991 years old:20 parent:fatherx school:university x 如何获得: name1 lastname1 13/03/1991 20 fatherx university x …对于不同的变量? 不要忘记用户有时

我有以下文本格式:

name:
last name:
birthday:
years old:
parent:
school:
我还有下一条消息

name:name1
last name:lastname1
birthday:13/03/1991
years old:20
parent:fatherx
school:university x
如何获得:

name1
lastname1
13/03/1991
20
fatherx
university x
…对于不同的变量? 不要忘记用户有时他们没有信息,例如他们有空的

parent:

在冒号上拆分。例如,如果您将每一行存储在一个单独的字符串中,您可以执行以下操作:

string s = "name:angel rodrigo";
string name= s.Split(':')[1];  // Get everything after the colon

在冒号上拆分。例如,如果您将每一行存储在一个单独的字符串中,您可以执行以下操作:

string s = "name:angel rodrigo";
string name= s.Split(':')[1];  // Get everything after the colon

您可以使用以下代码创建键值对字典

     List<string> fields = new List<string>
     {
        "name:",
        "last name:",
        "birthday:",
        "years old:",
        "parent:",
        "school:",
     };
     string rawData =
        @"name:angel rodrigo
last name:uc ku
birthday:13/03/1991
years old:20
parent:fernando uc puc
school:university x";

     var data = 
        fields.ToDictionary(
           field => field.TrimEnd (':'),
           field => Regex.Match(rawData, "(?<=" + Regex.Escape(field) + ").*"));
     foreach (var kvp in data)
     {
        Console.WriteLine(kvp.Key + "  =>  " + kvp.Value);
     }

您可以使用以下代码创建键值对字典

     List<string> fields = new List<string>
     {
        "name:",
        "last name:",
        "birthday:",
        "years old:",
        "parent:",
        "school:",
     };
     string rawData =
        @"name:angel rodrigo
last name:uc ku
birthday:13/03/1991
years old:20
parent:fernando uc puc
school:university x";

     var data = 
        fields.ToDictionary(
           field => field.TrimEnd (':'),
           field => Regex.Match(rawData, "(?<=" + Regex.Escape(field) + ").*"));
     foreach (var kvp in data)
     {
        Console.WriteLine(kvp.Key + "  =>  " + kvp.Value);
     }

@埃德:有一个问题,它被吸入了代码部分。信息在字符串变量中吗?我敢说……可能是正则表达式匹配组吗?@埃德:有一个问题,它被吸入了代码部分。信息在字符串变量中吗?我敢说……可能是正则表达式匹配组吗?什么意思[1]??有时用户不写日期,例如他们没有一所大学,这样做有效吗?
1
将在您指定的字符后获得所有内容(在给定的示例中)。如果有第二个字符,则表示第一个
和第二个
之间的所有内容。如果没有第二个
,它将在第一个
到字符串末尾之后获取所有内容。阅读和的更多信息以回答OPs问题,如果
后没有值,则不起作用。您需要对代码进行故障防护:1。什么是没有:?2.若字符串为空怎么办?[1]是什么意思??有时用户不写日期,例如他们没有一所大学,这样做有效吗?
1
将在您指定的字符后获得所有内容(在给定的示例中)。如果有第二个字符,则表示第一个
和第二个
之间的所有内容。如果没有第二个
,它将在第一个
到字符串末尾之后获取所有内容。阅读和的更多信息以回答OPs问题,如果
后没有值,则不起作用。您需要对代码进行故障防护:1。什么是没有:?2.如果字符串为空怎么办?