一个关于oc语言的题 大神请进 20
查找字符串@"today,wewillstudyanotherstring,thisstringisimportforus!"中是否含有@"string",输出它们的所有...
查找字符串@"today, we will study another string , this string is import for us!"中是否含有@"string", 输出它们的所有位置。
展开
1个回答
展开全部
NSString *string1 = @"today, we will study another string , this string is import for us!";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
---------------------------------------------或者
int)getSubStringShowNumsInStringBy:(NSString*)string andSubstring:(NSString*)Substring
{
int count =0;
NSRange range = [string rangeOfString:Substring];
if(range.length>0)
{
count++;
while (range.length>0) {
int startIndex =range.location+range.length;
int endIndex = string.length -startIndex;
string= [string substringWithRange:NSMakeRange(startIndex, endIndex)];
range = [string rangeOfString:Substring];
if(range.length>0)
{
count++;
}
}
}
return count;
}
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
---------------------------------------------或者
int)getSubStringShowNumsInStringBy:(NSString*)string andSubstring:(NSString*)Substring
{
int count =0;
NSRange range = [string rangeOfString:Substring];
if(range.length>0)
{
count++;
while (range.length>0) {
int startIndex =range.location+range.length;
int endIndex = string.length -startIndex;
string= [string substringWithRange:NSMakeRange(startIndex, endIndex)];
range = [string rangeOfString:Substring];
if(range.length>0)
{
count++;
}
}
}
return count;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询