0

ゲームのチャットを解析すると、「搾68 00 00 37 00 45 00 00」という文字列が表示されます

recipe = recipe.Replace("搾", "");
string[] rElements = new string[8];
rElements = recipe.Split(' ');
int num = int.Parse(rElements[0]);

最後の行で、理解できない Format 例外が発生します。入力文字列が正しい形式ではないことを示しています。デバッガーを確認したところ、最初の要素に「68」と表示されています。誰が何が起こっているのか手がかりを持っていますか?

4

3 に答える 3

2

提供された string を指定すると、コードは期待どおりに実行されます搾68 00 00 37 00 45 00 00num68 です。入力文字列と配列の最初の要素は、あなたが考えているものとは異なることを提案します。解析を試みる前に、それらを印刷してみてください。

于 2011-01-29T03:12:42.910 に答える
1

As noted already, given the string provided, your code will set num to 68. Here are a few pointers:

If you just want to remove the first character and don't need to match it, you can use:

recipe = recipe.Substring(1);

The Split method will create a new array with 8 elements, so there is no reason to initialize rElements with an array. Instead you can use:

var rElements = recipe.Split(' ');

If you need to convert all of the string entries in the rElements array into integers you can do this:

var numArray = rElements.Select(e => int.Parse(e)).ToArray();

Of course, if you need to check each one, you can use a loop with either TryParse or a try/catch. Putting it all together, you get:

var recipe = "搾68 00 00 37 00 45 00 00";
recipe = recipe.Substring(1);
var rElements = recipe.Split(' ');
var numArray = rElements.Select(e => int.Parse(e)).ToArray();
于 2011-01-29T07:18:24.697 に答える
0

入力文字列/コードを単にコピー/貼り付けただけだと思いますので、あなたが扱っている問題は入力文字列のエンコーディングだと思います。私の画面では、あなたの最初の文字が中国語の文字 zhà として表示されます。これは抑圧する、または抽出することを意味します。サンプルの入力文字列とコードは機能しますが、サンプルの入力文字列以外の別の Unicode 文字が後続の入力文字列に含まれている可能性がありますか?

REGEX を使用して不要な番号を削除してみてください。

using System.Text.RegularExpressions;
...
recipe = Regex.Replace(recipe, @"[^0-9\s]", string.Empty);
string[] rElements = recipe.Split(' ');
int num = int.Parse(rElements[0]);
于 2011-01-29T07:05:52.233 に答える