我有一个像@"abcdefghijklmnopqrstuvwxyzA"
这样的字符串。如您所见,A
即将结束。如何找到第一个大写字母并拆分字符串:
NSString *lower = @"abcdefghijklmnopqrstuvwxyz";
NSString *upper = @"A";
开头的字符串是静态的,因此大写字母可以是任何字母。这个扫描仪有帮助吗?
NSString *String = titleLabelLatestNews.text;
NSScanner *stringScanner = [NSScanner scannerWithString:String];
NSString *content = [[NSString alloc] init];
while ([stringScanner isAtEnd] == NO) {
[stringScanner scanUpToString:@"url=\"" intoString:Nil];
[stringScanner scanUpToString:@"/>" intoString:&content];
}
另一个例子,@"this is all lower case letters I am awesome";
应该变成两个字符串,@"this is all lower case letters";
和@"I am awesome";
明白了吗?大写字母前的任何内容都会转到字符串,然后转到另一个字符串。
答案 0 :(得分:2)
NSScanner
会为你做伎俩,是的。您只需创建一个由大写字母组成的NSCharacterSet
,然后使用scanUpToCharactersFromSet:intoString:
NSString * s = @"this is all lower case letters I am awesome";
NSScanner * scanner = [NSScanner scannerWithString:s];
NSString * firstPart;
[scanner scanUpToCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet]
intoString:&firstPart];
NSString * secondPart = [s substringFromIndex:[scanner scanLocation]];
答案 1 :(得分:1)
如果您坚持使用NSScanner,请使用scanCharactersFromSet:intoString:
,其中NSCharacterSet仅为小写字符。
如果有人关心的话,我个人会做的就是致电rangeOfCharacterFromSet(NSCharacterSet.uppercaseLetterCharacterSet()...)
并从那里得到所产生的子串。
答案 2 :(得分:1)
更好的解决方案是使用NSString' rangeOfCharacterFromSet
NSString *lowerCaseString=@"";
NSString *upperCaseString=@"";
NSString *stringToSplit = titleLabelLatestNews.text;
NSRange capitalRange=[stringToSplit rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
if (capitalRange.location == NSNotFound) {
lowerCaseString=stringToSplit;
}
else if (capitalRange.location ==0 ) {
upperCaseString=stringToSplit;
}
else {
lowerCaseString=[stringToSplit substringToIndex:capitalRange.location-1];
upperCaseString=[stringToSplit substringFromIndex:capitalRange.location];
}
NSLog(@"lower case string=%@ uppercase=%@",lowerCaseString,upperCaseString);
答案 3 :(得分:0)
为了完整性,正则表达式解决方案:
使用NSRegularExpression
如果您只对A-Z感兴趣,那么模式@"([^A-Z]*)([A-Z].*)"
将匹配您想要的大写字符(请参阅下面的unicode更改)。细分为两组,(...)
,一组用于之前;第一组 - 任何非大写的,[^A-Z]
,零次或多次,*
;第二组 - 大写字母[A-Z]
,后跟任何内容.*
。
使用firstMatchInString:options:range:
; NSTextCheckingResult
将包含两个匹配组的范围。
如果您希望允许使用Unicode无数的大写和标题字母,只需将上面的A-Z
更改为\\p{Lu}\\p{Lt}
(确保键入双反斜杠,即可将反斜杠传递给NSRegularExpression
)。这两个都是Unicode大写字母\\p{Lu}
和所有标题大小写字母\\p{Lt}
。
HTH
答案 4 :(得分:0)
使用componentsSeparatedByCharactersInSet:
将一个或多个解决方案投入到混合中,将字符串拆分为多个数组(如果需要,则超过2个):
// Separate the "sentence" into components separated
// by the characters in the uppercase character set
NSMutableArray *sentenceArray = [[sentence componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]] mutableCopy];
// Get the first sentence "segment", i.e. the sentenceArray's
// first object
NSString *segment = [sentenceArray objectAtIndex:0];
// Keep track of the character count with a variable
int characterCount = (int)segment.length;
// Then starting from sentenceArray's index 1, go through
// the rest of sentenceArray's indices
for (int i = 1 ; i < sentenceArray.count ; i ++) {
// Append that "separator" character to the segment at the
// current index by accessing the character before the current segment
segment = [[NSString stringWithFormat:@"%c", [sentence characterAtIndex:characterCount]]stringByAppendingString:[sentenceArray objectAtIndex:i]];
// Replace the object at the current index with this new segment
// string
[sentenceArray replaceObjectAtIndex:i withObject:segment];
// Increment the character count
characterCount += segment.length;
}
NSLog(@"%@", sentenceArray);
答案 5 :(得分:-1)
// Find index of first capital letter
NSInteger index = ^NSInteger{
for (NSInteger i = 0; i < string.length; ++i) {
unichar c = [string characterAtIndex:i];
if ('A' <= c && c <= 'Z') { return i; }
}
return string.length; // No capital letter, take the entire string
}();
NSLog(@"lower = %@", [string substringToIndex:index]);
NSLog(@"upper = %@", [string substringFromIndex:index]);