V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
ShikiSuen
V2EX  ›  iDev

我想在 Swift 内找到可以模拟 std::string_view 的方法

  •  
  •   ShikiSuen · 2022-05-07 13:27:34 +08:00 · 4092 次点击
    这是一个创建于 713 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教一个 Swift 问题:

    假设 strRawData 是某个文本档案的全部内容,用 pseudo code 说明一下:

    arrRawData = strRawData.components(separatedBy: " ") // 用一个 ASCII 空格分开

    for neta in arrRawData { 请问在这里该怎样获取 neta 相对于 strRawData 的 startIndex 和 endIndex ? (两者的类别都是 String.Index 或任何更适合的类型。) }

    问这个问题的原因是我想在 Swift 内找到可以模拟 std::string_view 的方法。

    7 条回复    2022-05-08 23:27:31 +08:00
    chipmuck
        1
    chipmuck  
       2022-05-07 14:22:51 +08:00
    ```swift
    let abc = "aaaa bbbb cccc dddd eeee ffff"

    let comps = abc.components(separatedBy: " ")
    for element in comps {
    let startIndex = abc.range(of: element)?.lowerBound
    let endIndex = abc.range(of: element)?.upperBound
    // ...
    }
    ```

    像这样?
    kaitok
        2
    kaitok  
       2022-05-07 17:50:31 +08:00
    @chipmuck 没写过 swift ,不过这样时间复杂度是不是有点高?
    MrKrabs
        3
    MrKrabs  
       2022-05-07 20:24:18 +08:00
    let array = ["a", "b", "", "c"]
    let str = array.joined(separator: " ")
    let splited = str.split(separator: " ", omittingEmptySubsequences: false)
    splited.forEach { sub in
    print(str.distance(from: str.startIndex, to: sub.startIndex), str[sub.startIndex..<sub.endIndex])
    }
    要省空间用 lazy split https://github.com/apple/swift-algorithms/blob/main/Guides/Split.md
    shoujiaxin
        4
    shoujiaxin  
       2022-05-07 21:02:30 +08:00
    用 split 试试?返回的是 Substring 的数组,Substring 和原 String 共享文本存储,Substring 的 startIndex 应该就是原 String 的索引
    ShikiSuen
        5
    ShikiSuen  
    OP
       2022-05-08 12:06:52 +08:00
    @shoujiaxin @MrKrabs
    谢谢二位。不知道这样处理是否可以正常处理包含 Emoji 的内容。

    ```
    let strData = #"""
    # Comment: These are test data
    treeNewBee 🌳🐮🍺 -0.1145141919810
    cowbeer 🐂🍺 -2.390804
    """#
    ```
    shoujiaxin
        6
    shoujiaxin  
       2022-05-08 13:14:44 +08:00
    @ShikiSuen Swift 的字符串是尽可能保证 Unicode 正确的
    ShikiSuen
        7
    ShikiSuen  
    OP
       2022-05-08 23:27:31 +08:00
    @shoujiaxin 太感謝了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2197 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:22 · PVG 08:22 · LAX 17:22 · JFK 20:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.