在NSString的圆括号之间提取值 [英] Extract values between parenthesis from NSString

查看:672
本文介绍了在NSString的圆括号之间提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSString,将像xxxx(yyyyy),其中x和y可以是任何字符。我想从括号内提取y。我设法使用NSScanner提取x,但我还没有想出正确的方法提取出y。

解决方案

只需完成:



如果您完全确定输出的格式,您可以使用数组方法:

  NSString * inputString; // this is in the formxxxx(yyyy)

NSCharacterSet * delimiters = [NSCharacterSet characterSetWithCharactersInString:@()];
NSArray * splitString = [inputString componentsSeparatedByCharactersInSet:delimiters];

NSString * xString = [splitString objectAtIndex:0];
NSString * yString = [splitString objectAtIndex:1];

当然,您需要确保定界字符不存在于inputString


I have an NSString that will be something like "xxxx (yyyyy)" where x and y can be any character. I'd like to extract just the y from inside the parenthesis. I managed to extract the x using a NSScanner but I haven't figured out the proper way to extract out the y.

解决方案

Just to be complete:

If you are absolutely sure of the format of your output you can use the array methods:

NSString *inputString; // this is in the form "xxxx(yyyy)"

NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"()"];
NSArray *splitString = [inputString componentsSeparatedByCharactersInSet:delimiters];

NSString *xString = [splitString objectAtIndex:0];
NSString *yString = [splitString objectAtIndex:1];

Of course, you need to be sure that the delimiting characters don’t exist in the inputString

这篇关于在NSString的圆括号之间提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆