如何找到两个字符串之间的子字符串?

时间:2022-09-11 19:18:27

I have a string "hi how are... you"

我有一串“嗨,你好吗……”你”

I want to find the Sub-string after how and before you..

我想在你之前和之后找到子字符串。

How to do this in objective c?

如何在objective c中做到这一点?

4 个解决方案

#1


81  

Find the range of the two strings and return the substring in between:

查找两个字符串的范围,返回中间的子字符串:

NSString *s = @"hi how are... you";

NSRange r1 = [s rangeOfString:@"how"];
NSRange r2 = [s rangeOfString:@"you"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString *sub = [s substringWithRange:rSub];

#2


9  

You could use the method of NSString substringWithRange

您可以使用NSString substringWithRange的方法

Example

例子

NSString *string=@"hi how are you";
NSRange searchFromRange = [string rangeOfString:@"how"];
NSRange searchToRange = [string rangeOfString:@"you"];
NSString *substring = [string substringWithRange:NSMakeRange(searchFromRange.location+searchFromRange.length, searchToRange.location-searchFromRange.location-searchFromRange.length)];
NSLog(@"subs=%@",substring); //subs= are

#3


0  

use SubstringTOIndex & SubstringFromIndex functions of NSString. Where SubstringFromIndex gives you the string from the index which you passed & SubstringToIndex function gives you the string upto the index which you passed.

使用NSString的SubstringTOIndex & SubstringFromIndex函数。当SubstringFromIndex给你从索引中传递的字符串时,SubstringToIndex函数为你提供了你通过的索引的字符串。

Also try substringWithRange function which returns you the string between the range which you passed.

还可以尝试使用substringWithRange函数,它返回您传递的范围之间的字符串。

#4


-1  

Use substringWithRange...

使用substringWithRange……

NSString* substring = [originalString substringWithRange:NSMakeRange(3, originalString.length-6)];

#1


81  

Find the range of the two strings and return the substring in between:

查找两个字符串的范围,返回中间的子字符串:

NSString *s = @"hi how are... you";

NSRange r1 = [s rangeOfString:@"how"];
NSRange r2 = [s rangeOfString:@"you"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString *sub = [s substringWithRange:rSub];

#2


9  

You could use the method of NSString substringWithRange

您可以使用NSString substringWithRange的方法

Example

例子

NSString *string=@"hi how are you";
NSRange searchFromRange = [string rangeOfString:@"how"];
NSRange searchToRange = [string rangeOfString:@"you"];
NSString *substring = [string substringWithRange:NSMakeRange(searchFromRange.location+searchFromRange.length, searchToRange.location-searchFromRange.location-searchFromRange.length)];
NSLog(@"subs=%@",substring); //subs= are

#3


0  

use SubstringTOIndex & SubstringFromIndex functions of NSString. Where SubstringFromIndex gives you the string from the index which you passed & SubstringToIndex function gives you the string upto the index which you passed.

使用NSString的SubstringTOIndex & SubstringFromIndex函数。当SubstringFromIndex给你从索引中传递的字符串时,SubstringToIndex函数为你提供了你通过的索引的字符串。

Also try substringWithRange function which returns you the string between the range which you passed.

还可以尝试使用substringWithRange函数,它返回您传递的范围之间的字符串。

#4


-1  

Use substringWithRange...

使用substringWithRange……

NSString* substring = [originalString substringWithRange:NSMakeRange(3, originalString.length-6)];