1

この文字列があり、最初の "=" に基づいて 2 に分割するMIL_A_OP=LI_AND=SSB12=JL45==DO=90==IT=KR002112 必要があります

だから私はそれを取得する必要があります:

最初の文字列:MIL_A_OP

2 番目の文字列: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);
}
4

3 に答える 3

6

返す要素の最大数を指定できる Split() メソッドのオーバーロードが必要です。これを試して:

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

ドキュメンテーションはこちら

マシューのフィードバックで更新されました。

于 2012-04-16T16:46:05.230 に答える
5

以下を試してください

string all = "MIL_A_OP=LI_AND=SSB12=JL45==DO=90==IT=KR002112";
int index = all.IndexOf('=');
if (index < 0) {
  throw new Exception("Bad data");
}
var first = all.Substring(0, index);
var second = all.Substring(index + 1, all.Length - (index + 1));
于 2012-04-16T16:46:05.773 に答える
2

行に常に = 文字が含まれている場合、次のように動作するはずです

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

if (line_split1.Length != 2)
    throw new Exception ("Invalid format");
于 2012-04-16T16:54:21.970 に答える