按字符串数组拆分长字符串

时间:2018-03-13 10:17:00

标签: c# arrays string

我有一个十六进制输入字符串-(IBAction)hideSecondView:(id)sender{ dispatch_async(dispatch_get_main_queue(), ^{ secondView.hidden = YES; }); } 。我想根据我编写的文本框输入拆分输入字符串,如下所示:

"0000095700012A27"

它将根据我的String txtbox_input = "-2,4,-4,4,2"; String pattern = ","; String[] splitters = Regex.Split(txtbox_input, pattern); 进行拆分,基本上,我希望我的输出为:

txtbox_input

一路上,是否可以确定00,0009,5700,012A,27 好像它是否为负,它会跳过转换?正如我想要的那样,如果数字为正数,我的分割数据将被转换为以下数据。

txtbox_input

1 个答案:

答案 0 :(得分:0)

如果我们想忽略所有的弊端,从而返回2, 4, 4, 4, 2个字符长的后续块

  String source = "0000095700012A27";
  String txtbox_input = "-2,4,-4,4,2";
  String pattern = ",";

  var data = txtbox_input
    .Split(new string[] { pattern }, StringSplitOptions.RemoveEmptyEntries)
    .Select(item => Math.Abs(int.Parse(item))) // Math.Abs - ignoring minus
    .Select(length => {
      string item = source.Substring(0, length);
      source = source.Substring(length);

      return item;
    })
 // .Select(item => Convert.ToUInt16(item, 16)) // if you want UInt16 items
    .ToArray(); // Let's have an array of chunks

  Console.Write(string.Join(",", data));

结果:

  00,0009,5700,012A,27