代码之家  ›  专栏  ›  技术社区  ›  Nazmul Hasan

如何按给定大小的块从字符串拆分到数组

  •  5
  • Nazmul Hasan  · 技术社区  · 6 年前

    我想按给定大小的块分割字符串 2

    例子:

    一串 "1234567" 输出应为 ["12", "34", "56","7"]

    9 回复  |  直到 3 年前
        1
  •  9
  •   Martin R    2 年前

    您可以将集合元素(在本例中为字符)按每n个元素分组,如下所示:

    extension Collection {
        func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
            sequence(state: startIndex) { start in
                guard start < self.endIndex else { return nil }
                let end = self.index(start, offsetBy: maxLength, limitedBy: self.endIndex) ?? self.endIndex
                defer { start = end }
                return self[start..<end]
            }
        }
        func subSequences(of n: Int) -> [SubSequence] {
            .init(unfoldSubSequences(limitedTo: n))
        }
    }
    

    let numbers = "1234567"
    let subSequences = numbers.subSequences(of: 2)
    print(subSequences)    // ["12", "34", "56", "7"]
    

    编辑/更新 :

    如果要将超出的字符附加到最后一组:

    extension Collection {
        func unfoldSubSequencesWithTail(lenght: Int) -> UnfoldSequence<SubSequence,Index> {
            let n = count / lenght
            var counter = 0
            return sequence(state: startIndex) { start in
                guard start < endIndex else { return nil }
                let end = index(start, offsetBy: lenght, limitedBy: endIndex) ?? endIndex
                counter += 1
                if counter == n {
                    defer { start = endIndex }
                    return self[start...]
                } else {
                    defer { start = end }
                    return self[start..<end]
                }
            }
        }
        func subSequencesWithTail(n: Int) -> [SubSequence] {
            .init(unfoldSubSequencesWithTail(lenght: n))
        }
    }
    

    let numbers = "1234567"
    let subSequencesWithTail = numbers.subSequencesWithTail(n: 2)
    print(subSequencesWithTail)    // ["12", "34", "567"]
    
        2
  •  2
  •   Naresh    6 年前
    var testString = "abcdefghijklmnopqrstu"
    var startingPoint: Int = 0
    var substringLength: Int = 1
    var substringArray = [AnyHashable]()
    for i in 0..<(testString.count ?? 0) / substringLength {
        var substring: String = (testString as NSString).substring(with: NSRange(location: startingPoint, length: substringLength))
        substringArray.append(substring)
        startingPoint += substringLength
    }
    print("\(substringArray)")
    

    输出: ( 一 b c d e f g、, h、, 我, j k l、, m, n o、, p q r s t, u )

        3
  •  2
  •   iOS Geek    6 年前

    试试这个

    func SplitString(stringToBeSplitted:String, By:Int) -> [String]
        {
            var newArray = [String]()
            var newStr = String()
            for char in stringToBeSplitted
            {
                newStr += String(char)
                if newStr.count == By
                {
                    newArray.append(newStr)
                    newStr = ""
                }
    
            }
            return newArray
        }
    
        4
  •  2
  •   Jano teo    4 年前

    Swift 5

    extension Array {
        func chunks(size: Int) -> [[Element]] {
            return stride(from: 0, to: count, by: size).map {
                Array(self[$0 ..< Swift.min($0 + size, count)])
            }
        }
    }
    
    extension String {
        func chunks(size: Int) -> [String] {
            map { $0 }.chunks(size: size).compactMap { String($0) }
        }
    }
    
    let s = "1234567"
    print(s.chunks(size: 2)) // ["12", "34", "56", "7"]
    
        5
  •  1
  •   Shahrukh    6 年前
    extension String {
        func split(len: Int) -> [String] {
            var currentIndex = 0
            var array = [String]()
            let length = self.characters.count
            while currentIndex < length {
                let startIndex = self.startIndex.advancedBy(currentIndex)
                let endIndex = startIndex.advancedBy(len, limit: self.endIndex)
                let substr = self.substringWithRange(Range(start: startIndex, end: endIndex))
                array.append(substr)
                currentIndex += len
            }
            return array
        }
    }
    

    "123456789".拆分(2)

    //输出:[“12”、“34”、“56”、“78”、“9”]

        6
  •  1
  •   Ketan Parmar    6 年前

    我在目标c中写了一个方法,如下所示,

    -(NSMutableArray*)splitString : (NSString*)str withRange : (int)range{
    
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    
    NSMutableString *mutableStr = [[NSMutableString alloc]initWithString:str];
    
    int j = 0;
    int counter = 0;
    
    for (int i = 0; i < str.length; i++) {
    
        j++;
    
        if (range == j) {
    
            j = 0;
    
    
            if (!(i == str.length - 1)) {
    
                 [mutableStr insertString:@"$" atIndex:i+1+counter];
            }
    
    
    
            counter++;
        }
    }
    
    
    
    arr = (NSMutableArray*)[mutableStr componentsSeparatedByString:@"$"];
    
    NSLog(@"%@",arr);
    
    return arr;
    }
    

    可以这样调用此方法,

     [self splitString:@"123456" withRange:2];
    

    结果是,

    (
    12,
    34,
    56
    )
    
        7
  •  1
  •   Yogi    6 年前

    您还可以尝试以下代码:

    var arrStr: [Substring] = []
        let str = "1234567"
        var i = 0
        while i < str.count - 1 {
            let index = str.index(str.startIndex, offsetBy: i)
            //Below line gets current index and advances by 2
            let substring = str[index..<str.index(index, offsetBy: 2)]
            arrStr.append(substring)
            i += 2
        }
        if str.count % 2 == 1 {
            arrStr.append(str.suffix(1))
        }
        print(arrStr)
    
        8
  •  1
  •   Tim    6 年前

    有一种愚蠢的方法,您可以考虑数据模型的规则。

        var strOld = "123456"
        print("The original string:\(strOld)")
    
        strOld.insert("、", at: strOld.index(before: strOld.index(strOld.startIndex, offsetBy: 3)))
    
        strOld.insert("、", at: strOld.index(before: strOld.index(strOld.startIndex, offsetBy: 6)))
    
        print("After inserting:\(strOld)")
    
        let str = strOld
    
        let splitedArray = str.components(separatedBy: "、")
        print("After the split of the array:\(splitedArray)")
    
        let splitedArrayOther = str.split{$0 == "、"}.map(String.init)
        print("After break up the array (method 2):\(splitedArrayOther)")
    

    结果如下:

    The original string:123456
    After inserting:12、34、56
    After the split of the array:["12", "34", "56"]
    After break up the array (method 2):["12", "34", "56"]
    
        9
  •  1
  •   PDK    3 年前

    由于递归,这里有一个简短(干净)的解决方案:

    extension Collection {
        func chunks(ofSize size: Int) -> [SubSequence] {
            // replace this by `guard count >= size else { return [] }`
            // if you want to omit incomplete chunks
            guard !isEmpty else { return [] }
            return [prefix(size)] + dropFirst(size).chunks(ofSize: size)
        }
    }
    

    递归不应该造成性能问题,因为Swift支持尾部调用优化。

    此外,如果Swift数组在预编或附加元素时非常快(如Objective-C数组),那么数组操作应该很快。

    因此,您可以获得快速且可读的代码(假设我的数组假设为真)。