c# - 只有第一次出现

标签 c# .net string split

我有这个字符串 MIL_A_OP=LI_AND=SSB12=JL45==DO=90==IT=KR002112 我需要根据第一个 "="

split it into 2

所以我需要它来获得:

第一个字符串:MIL_A_OP

第二个字符串:LI_AND=SSB12=JL45==DO=90==IT

下面的代码是我所拥有的,但它给了我 MIL_A_OP 和 LI_AND,我想念其余的

try
{
    StreamReader file1 = new StreamReader(args[0]);
    string line1;
    while ((line1 = file1.ReadLine()) != null)
    {
        if (line1 != null && line1.Trim().Length > 0)//if line is not empty
        {
            int position_1 = line1.IndexOf('=');
            string s_position_1 = line1.Substring(position_1, 1);
            char[] c_position_1 = s_position_1.ToCharArray(0,1);
            string[] line_split1 = line1.Split(c_position_1[0]);
            Foo.f1.Add(line_split1[0], line_split1[1]);
        }
    }
    file1.Close();
}
catch (Exception e)
{
    Console.WriteLine("File " + args[0] + " could not be read");
    Console.WriteLine(e.Message);
}

最佳答案

您需要 Split() 方法的重载,该方法允许您指定要返回的元素的最大数量。试试这个:

string[] line_split1 = line1.Split( new char[]{'='}, 2 );

Documentation here .

更新了 Matthew 的反馈。

关于c# - 只有第一次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178211/

相关文章:

c# - 要在 HTML 控件中显示的 XML 元素中的空格

c# - 在csproj中使用appdata环境变量

c - 递归 C 字符串替换

swift - 无法将类型 '[String]?' 的值转换为预期的参数类型 'String' Swift

c# - 我可以在运行时将 ConnectionStrings 添加到 ConnectionStringCollection 吗?

c# - List vs ArrayList vs Dictionary vs Hashtable vs Stack vs Queue?

c# - 使用 iTextSharp 打开受密码保护的 pdf 文件

c# - 如何运行在项目中添加的exe

.net - 使用 .Net 检查文件系统容量的最佳方法是什么?

Python:列表中的列表 - 将字符串添加到父列表中的项目而不影响子列表的函数?