Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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语言中提取字符串部分#_C#_Parsing - Fatal编程技术网

C# 在c语言中提取字符串部分#

C# 在c语言中提取字符串部分#,c#,parsing,C#,Parsing,我需要解析列中的字符串记录,将其分为两部分。我需要分解的两个记录示例如下: 第3.1.1行 Qa.2.1 对于每个记录,我需要最后一个“.”之前的所有内容,以及最后一个“.”之后的所有内容。例如,对于上面的#1,我需要: 代码=第3.1行 右值=1 我是c#的新手,尝试了以下几点,但没有达到预期效果: string ID = "Row.3.1.1; int CodeIndex = ID.LastIndexOf("."); string Code = ID.Substring(CodeIndex

我需要解析列中的字符串记录,将其分为两部分。我需要分解的两个记录示例如下:

  • 第3.1.1行
  • Qa.2.1
  • 对于每个记录,我需要最后一个“.”之前的所有内容,以及最后一个“.”之后的所有内容。例如,对于上面的#1,我需要:

    代码=第3.1行
    右值=1

    我是c#的新手,尝试了以下几点,但没有达到预期效果:

    string ID = "Row.3.1.1;
    
    int CodeIndex = ID.LastIndexOf(".");
    string Code = ID.Substring(CodeIndex);
    
    int ValueIndex = ID.IndexOf(".");
    Rstring RValue = ID.Substring(ValueIndex);
    

    我认为这就是您要寻找的代码:

    string ID = "Row.3.1.1";
    int CodeIndex = ID.LastIndexOf(".");
    string Code = ID.Substring(0, CodeIndex);
    string RValue = ID.Substring(CodeIndex + 1);
    
    请参阅.Net代码

    另外,您可能需要阅读MSDN上的函数。

    尝试以下方法:

    class Program
        {
            static void Main(string[] args)
            {
                var firstTest = "Row.3.1.1";
                var secondTest = "Qa.2.1";
    
                Console.WriteLine(BuildFromString(firstTest));
                Console.WriteLine(BuildFromString(secondTest));
    
                Console.Read();
            }
    
            public static Extract BuildFromString(string input)
            {
                return new Extract
                {
                    Code = input.Substring(0, input.LastIndexOf('.')),
                    RValue = input.Substring(input.LastIndexOf('.'))
                };
            }
    
            public class Extract
            {
                public string Code { get; set; }
                public string RValue { get; set; }
    
                public override string ToString()
                {
                    return $"Code: {Code} RValue:{RValue}";
                }
            }
        }
    

    您不需要两个索引操作。(如果我正确阅读了要求)


    很好的努力,但是你的指数有点偏离

    string ID = "Row.3.1.1";
    
    int CodeIndex = ID.LastIndexOf(".");
    
    //get the first half which is from index 0 (the start) to where the last `.` was found
    string Code = ID.Substring(0, CodeIndex);
    
    //start from where the last '.' and move up one because we 
    //don't want to include the `.` in RValue
    string RValue = ID.Substring(CodeIndex + 1); 
    
    您可能希望包括错误处理等(例如,如果字符串中没有
    ,会发生什么情况)。但是假设你有一个完美的字符串,上面的代码应该可以工作。

    你很接近:

    string ID = "Row.3.1.1;
    
    int CodeIndex = ID.LastIndexOf(".");
    string Code = ID.Substring(CodeIndex);
    
    // int ValueIndex = ID.IndexOf(".");
    Rstring RValue = ID.Substring(0, CodeIndex); //Here, (0, CodeIndex)
    

    没有子字符串和lastindexof的另一种可能性:

    var IDParts = ID.Split('.');
    var RValue = IDParts[IDParts.Length-1];
    var Code = ID.TrimEnd("." + RValue);
    

    对于任何答案中的所有解决方案,请注意,如果您的ID中没有“.”,它们会抛出异常,或者在我的情况下会产生错误的结果。因此您应该添加一个检查。

    正则表达式使这变得容易。不需要索引或偏移

    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^(.*)\.([^\.]*)$");
    
    string testString= "Row.3.1.1";
    System.Text.RegularExpressions.GroupCollection groups = regex.Match(testString).Groups;
    System.Console.WriteLine("Code = " + groups[1]);
    System.Console.WriteLine("RValue = " + groups[2]);
    

    那个代码示例不会编译——这个问题是与编译有关还是与实际结果有关?请发布不正确的结果、错误消息或在试用期间让您感到悲伤的一切。现在很难确切知道问题出在哪里。右值可能不止是一个字符。但只有OP知道……代码和右值是相反的
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^(.*)\.([^\.]*)$");
    
    string testString= "Row.3.1.1";
    System.Text.RegularExpressions.GroupCollection groups = regex.Match(testString).Groups;
    System.Console.WriteLine("Code = " + groups[1]);
    System.Console.WriteLine("RValue = " + groups[2]);