简体   繁体   English

如何将八个字符串分成四个变量?

[英]How can I split an eight character string into four variables?

I hava a string like this: 我有一个像这样的字符串:

var id = "01020304";

Is there a simple way I can split this into four variables called pr, pa, fo, it Each variable needs to have the two characters of the string. 有没有一种简单的方法可以将其分为四个分别称为pr,pa,fo的变量,所以每个变量都需要具有字符串的两个字符。 Looking for an elegant solution if it exists. 寻找一个优雅的解决方案(如果存在)。

You can use Substring : 您可以使用Substring

pr = id.Substring(0, 2);
pa = id.Substring(2, 2);
fo = id.Substring(4, 2);
it = id.Substring(6, 2);

Since you have four distinct variables, you can't get much more elegant than using Substring: 由于您有四个不同的变量,因此使用Substring不会比它优雅得多:

var pr = id.Substring(0, 2);
var pa = id.Substring(2, 2);
var fo = id.Substring(4, 2);
var it = id.Substring(6);

Were you looking for an array of four 2-character substrings, you could get fancier: 如果您正在寻找包含四个2个字符的子字符串的数组,则可能会更有趣:

var parts = new string[4];
for (int i = 0 ; i != parts.Length ; i ++) {
    parts[i] = id.Substring(2*i, 2);
}

EDIT: The same can be done with a LINQ expression: 编辑:可以使用LINQ表达式完成相同操作:

var parts = Enumerable
    .Range(0, id.Length/2)
    .Select(i => x.Substring(2*i, 2))
    .ToArray();

Try regular expression. 尝试正则表达式。

 var id = "01020304";
 string pat = "(?:(\\d{2}))";

 var result = Regex.Split(id, pat).Where(p=>p!=string.Empty);
 foreach (var t in result)
 {
   Console.WriteLine(t);
 }

If you are always going to have an input of 8 characters and always require 4 variables, you can simply split the string with the Substring(...) method: 如果您总是要输入8个字符并且总是需要4个变量,则可以使用Substring(...)方法简单地分割字符串:

var id = "01020304";
string pr = id.Substring(0, 2);
string pa = id.Substring(2, 2);
string fo = id.Substring(4, 2);
string it = id.Substring(6, 2);

Otherwise, you can employ a method of running through a for loop and splitting off two characters at a time. 否则,您可以采用一种遍历for循环并一次拆分两个字符的方法。

Here is a solution with a loop, which would support strings of this format, any length: 这是一个带循环的解决方案,它将支持这种格式的字符串,任何长度:

string id  = "01020304";
int length = id.Length;

int[] holder = new int[length/2];

for (int i = 0; i < length/2; i++) {
    holder[i] = id.Substring(i*2, 2);
}

Here is a version using Linq. 这是使用Linq的版本。

Interestingly enough, this is not so easy to achieve with just the built-in operators. 有趣的是,仅使用内置运算符并非易事。 The version below just uses the Linq extension methods that come with the .NET framework: 下面的版本仅使用.NET框架随附的Linq扩展方法:

var result = "01020304".ToCharArray().
           Select((c,i) => new { idx = i % 2 == 1 ? i - 1 : i, ch = c }).
           GroupBy(e => e.idx, 
              (k,g) => new String(g.Select(e => e.ch).ToArray()));

If you use the morelinq Extensions the query can be simplified to 如果使用morelinq扩展,则查询可以简化为

 var result = "01020304".ToCharArray().
                     Batch(2).Select(ca => new String(ca.ToArray()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM