gpt4 book ai didi

objective-c - 获取行首的空格数

转载 作者:行者123 更新时间:2023-12-04 06:00:02 24 4
gpt4 key购买 nike

我尝试了以下实现:

NSString *string = @"    Test";
NSString *spaces = @"";

NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&spaces];

空格的数量应该是 [spaces length] .但是,我尝试的所有测试总是返回 0 .

有任何想法吗?

最佳答案

使用 setCharactersToBeSkipped: .文档说,

The default set of characters to skip is the whitespace and newline character set.



所以它只是默默地跳过空白前缀。

还有另一种方法可以做到:
NSUInteger prefixLength = [string rangeOfCharacterFromSet: 
[NSCharacterSet whitespaceCharacterSet]];

关于objective-c - 获取行首的空格数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com