2013-04-06 49 views
2

我是新客观的,我面临第一个问题,我无法继续我的第一个项目。NSString子串后的子串

这是很简单的,我有一个的NSString:

NSString *myString = @"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>"; 

我想要做的是让大小“25”这始终是2字符长的值,所以我可以计算我的UILabel大小。

我知道如何检测是否有我要找的串“大小=”使用:

if ([string rangeOfString:@"bla"].location == NSNotFound) 

,但我还没有找到或者不知道如何提取字符串@“大小= XX”然后从* myString获取XX作为NSString

感谢您的任何帮助。

+0

一般来说,你可以使用“正则表达式”,或者你可以使用componentsSeparatedByString。对于后者,您可能需要使用hasPrefix检查每个生成的“组件”以查找以“size =”开头的那个。 – 2013-04-06 12:20:39

回答

2
NSString *myString = @"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>"; 
NSRange range = [myString rangeOfString:@"size="]; 
if (range.location != NSNotFound) 
{ 
    NSLog(@"Found \"size=\" at %d", range.location); 
    NSString *sizeString = [myString substringWithRange:NSMakeRange(range.location+5, 2)]; 
    NSLog(@"sizeString: %@", sizeString); 
} 


这应该做的伎俩一个数字。你也可以在最后做:int sizeFont = [sizeString intValue];

+0

非常感谢您的提示大卫! – user2252092 2013-04-06 15:16:03

+0

请注意,如果未找到目标字符串,rangeOfString将返回长度== 0。所以检查长度比位置更好。如果接收者为零,则长度和位置都返回为0,而不是NSNotFound。 – Reefwing 2017-12-12 06:35:55

2
NSString *myString = @"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>"; 
    if ([myString rangeOfString:@"size"].location != NSNotFound) 
    { 
     myString = [myString substringFromIndex:[myString rangeOfString:@"size"].location]; 
     myString = [myString substringToIndex:[myString rangeOfString:@" "].location]; // Now , myString ---> size=25 color='#d79198'> Here is some text !</font> 
     myString = [myString substringFromIndex:[myString length]-2];// Now, myString ---> size=25 
     NSLog(@"myString -- %@",myString); // Now, myString ---> 25 
    } 
+0

就像你试图[使用正则表达式解析HTML]一样(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) 。你**不应该** – 2013-04-06 12:07:04

+0

@ H2CO3嗯......正则表达式...... *看起来内疚* – 2013-04-06 12:13:08

+0

完美,非常感谢你! – user2252092 2013-04-06 15:15:44

0

HTML页面的一部分?然后use the tool that is designed for the task

+3

这个答案有什么问题?这是迄今为止唯一正确的答案**。 – 2013-04-06 12:21:10

+0

这也是我的意见... – HAS 2013-04-06 12:36:10

+0

非我使用这个,因为有色UILabel – user2252092 2013-04-06 15:16:56

0

你可以自己计算数量的范围,或者使用一个非常简单的正则表达式来获取子,像

(?<=size\=)\d* 

这意味着,你正在寻找的是前面有个数字(\d*)“大小=”((?<=size\=)

使用哪会NSRegularExpression是

NSError *error = NULL; 
NSRegularExpression *regex = 
    [NSRegularExpression regularExpressionWithPattern:@"(?<=size\\=)\\d*" 
              options:0 
               error:&error]; 
NSTextCheckingResult *match = 
    [regex firstMatchInString:myString 
        options:0 
         range:NSMakeRange(0, [myString length])]; 
NSString *sizeText = [myString substringWithRange:match.range]; 

最后,你应该CONVER t为文本"25"到使用

NSInteger size = [sizeText integerValue]; 
+0

我第一次使用NSRegularExpression,感谢提示大卫。 – user2252092 2013-04-06 15:17:59

-1

使用componentsSeparatedByString:方法...

NSString *myString = @"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>"; 
    NSString *theSizeString = [[[[myString componentsSeparatedByString:@" "] objectAtIndex:2] componentsSeparatedByString:@"="] objectAtIndex:1]; 
    NSLog(@"The sizestring:%@",theSizeString); 

我认为这将是对你有所帮助。

+0

也在工作,非常感谢你 – user2252092 2013-04-06 15:16:20

-1

您可以得到字符串@"size="的范围。范围有位置和长度。所以你下一步需要调用myStringsubstringWithRange:方法。该参数将是一个NSRage从2.

0

位置+的@"size="长度和长度开始如果有字符串等堆栈:溢出然后用它如下:

NSString *[email protected]"stack:overflow" 
NSString *one = [[Base componentsSeparatedByString:@":"] objectAtIndex:0]; 
NSString *two = [[Base componentsSeparatedByString:@":"] objectAtIndex:1]; 

在这种情况下一个堆栈=和两个=溢出