gpt4 book ai didi

iphone - 包含允许字符的字符串

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:32 24 4
gpt4 key购买 nike

是否有一种干净的方法来获取只包含允许字符的字符串?

例如:

NSString* myStr = @"a5&/Öñ33";
NSString* allowedChars = @"abcdefghijklmnopqrstuvwxyz0123456789";
NSString* result = [myStr stringWIthAllowedChrs:allowedChars];

结果现在应该是 @"a533";

最佳答案

这不是最干净的,但您可以使用字符集分隔字符串,然后使用空字符串组合生成的数组。

// Create a character set with every character not in allowedChars
NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:allowedChars] invertedSet];
// Split the original string at any occurrence of those characters
NSArray *splitString = [myStr componentsSeparatedByCharactersInSet:charSet];
// Combine the result into a string
NSString *result = [splitString componentsJoinedByString:@""];

关于iphone - 包含允许字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7741681/

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