在c#中提取字符串部分

时间:2016-07-19 22:48:11

标签: c# parsing

我需要解析一列中的字符串记录,将其分解为两部分。我需要分解的2个记录的例子是:

  1. Row.3.1.1
  2. Qa.2.1
  3. 对于每条记录,我要求在最后一个记录之前提供所有内容。'以及最后一次之后的所有事情。'例如对于上面的#1,我需要:

    代码= Row.3.1
    RValue = 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);
    

7 个答案:

答案 0 :(得分:0)

试试这个方法:

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}";
            }
        }
    }

答案 1 :(得分:0)

努力工作但你的指数略有下降。

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); 

您可能希望包含错误处理等(例如,如果字符串中没有.。会发生什么)。但假设你有完美的字符串,上面的代码应该可以工作。

答案 2 :(得分:-1)

我认为这是您正在寻找的代码:

string ID = "Row.3.1.1";
int CodeIndex = ID.LastIndexOf(".");
string Code = ID.Substring(0, CodeIndex);
string RValue = ID.Substring(CodeIndex + 1);

请参阅.Net Fiddle代码

此外,您可能希望阅读MSDN上的Substring功能。

答案 3 :(得分:-1)

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

   string ID = "Row.3.1.1";
   int codeIndex = ID.LastIndexOf(".");
   string code = ID.Substring(0, codeIndex);
   string rValue = ID.Substring(codeIndex + 1);

答案 4 :(得分:-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)

答案 5 :(得分:-1)

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

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

对于任何答案中的所有解决方案,请注意,如果没有“。”,他们会抛出异常或在我的情况下产生错误结果。在你的身份证中。所以你应该为它添加一个检查。

答案 6 :(得分:-1)

正则表达式使这很容易。无需索引或抵消。

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]);