Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何计算一个字符串中的字符数直到分号_Ios_Objective C_Nsstring - Fatal编程技术网

Ios 如何计算一个字符串中的字符数直到分号

Ios 如何计算一个字符串中的字符数直到分号,ios,objective-c,nsstring,Ios,Objective C,Nsstring,我在NSDictionary中有一个字符串值,看起来像这样 Rule = "K-------;-KKCT---"; NSString *tempString = [NSString alloc] init]; tempString = [myDictionary objectForKey:@"Rule"]; 如果我做了这样的事 Rule = "K-------;-KKCT---"; NSString *tempString = [NSString alloc] init]; tempStr

我在
NSDictionary
中有一个字符串值,看起来像这样

Rule = "K-------;-KKCT---";
NSString *tempString = [NSString alloc] init];
tempString = [myDictionary objectForKey:@"Rule"];
如果我做了这样的事

Rule = "K-------;-KKCT---";
NSString *tempString = [NSString alloc] init];
tempString = [myDictionary objectForKey:@"Rule"];

tempString现在有如下值“K-------KKCT---”,我想知道如何计算在本例中分号之前有多少个字符。

下面的
NSString
方法就是您要找的

-(NSRange)rangeOfString:(NSString*)aString


这将获得您提供的字符串的第一个实例的“范围”(在本例中为分号)

下面的
NSString
方法就是您要寻找的

-(NSRange)rangeOfString:(NSString*)aString

这将获得您提供的字符串的第一个实例的“范围”(在本例中为分号)

尝试一下:

NSString *tempString = myDictionary[@"Rule"];
NSUInteger count = [tempString rangeOfString:@";"].location;
if (count == NSNotFound) {
    count = 0;
}
请注意,您不应该仅仅为了从字典中为变量分配另一个值而分配空字符串

如果字符串中没有分号,结果将是特殊值
NSNotFound
。上面的代码将其转换为0的计数。根据需要调整此逻辑。

尝试一下:

NSString *tempString = myDictionary[@"Rule"];
NSUInteger count = [tempString rangeOfString:@";"].location;
if (count == NSNotFound) {
    count = 0;
}
请注意,您不应该仅仅为了从字典中为变量分配另一个值而分配空字符串


如果字符串中没有分号,结果将是特殊值
NSNotFound
。上面的代码将其转换为0的计数。根据需要调整此逻辑。

您也可以执行以下操作:

NSArray *subStrings = [tempString componentsSeparatedByString: @";"];
int firstStringLength = [[subStrings objectAtIndex: 0] length];

如果需要查找所有子字符串的长度,只需在数组中循环。

也可以执行以下操作:

NSArray *subStrings = [tempString componentsSeparatedByString: @";"];
int firstStringLength = [[subStrings objectAtIndex: 0] length];
如果需要找到所有子字符串的长度,只需在数组中循环