How to split a string into a fixed length string array?

前端 未结 8 1866
别跟我提以往
别跟我提以往 2020-12-21 02:10

I have a long string like this

dim LongString as String = \"123abc456def789ghi\"

And I want to split it into a string array. Each element

8条回答
  •  感情败类
    2020-12-21 02:23

    You could use LINQ like so:

    
    ' VB.NET
    Dim str = "123abc456def789ghij"
    Dim len = 3
    Dim arr = Enumerable.Range(0, str.Length / len).Select (Function(x) str.Substring(x * len, len)).ToArray()
    
    
    
    // C#
    var str = "123abc456def789ghij";
    var len = 3;
    var arr = Enumerable.Range(0, str.Length / len).Select (x => str.Substring(x * len, len)).ToArray();
    
    

    Note this will only take complete occurrences of length (i.e. 3 sets in a string 10 characters long).

提交回复
热议问题