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
Easiest way would be to use RegExKit:
http://regexkit.sourceforge.net/
Then you'd do something like:
[#"xxxx(yyyyy)" getCapturesWithRegexAndReferences:#"\\((.*)\\)",#"$1", &extractedString,nil];
and extractedString would contain whatever was in parenthesis.
Scan up to the ‘(’, then scan it, then scan up to the ')'. The result of the last scan is yyyy.
you might have a look at PKTokenizer in ParseKit:
http://parsekit.com
Related
I would like to search for a specific last character in a NSString and couldn't figure out a way. In C# for example, I can do lastindexof to get the index of the char I'm searching for and then use substring to get what I want. Is there a way in Obj C to achieve this?
NSString *s = #"aabbcc";
[s rangeOfString:#"b" options:NSBackwardsSearch]; // {3, 1}
What is the shortest way to create a string with spaces and desired length in Objective-C?
I can do it using for cycle, but it's several lines of code.
Returns first six characters
NSString *fullName = #"Warif Akhand Rishi";
NSString *subString = [fullName substringToIndex:6];
Have you looked at stringWithFormat ?
[NSMutableString stringWithFormat:#"%20d", 42];
How can I return the first three characters of an NSString?
mystr=[mystr substringToIndex:3];
Be sure your string has atleast 3 ch.. o.e. it will crash the app.
Here are some other links to check NSsting operations...
Link1
Link2
Apple Link
First, you have to make sure that the string contains at least 3 characters:
NSString *fullString = /* obtain from somewhere */;
NSString *prefix = nil;
if ([fullString length] >= 3)
prefix = [fullString substringToIndex:3];
else
prefix = fullString;
substringToIndex: will throw an exception if the index you provide is beyond the end of the string.
the right way is:
text = [text substringToIndex:NSMaxRange([text rangeOfComposedCharacterSequenceAtIndex:2])];
substringToIndex of NSString is indexing by code unit, emoji takes two code units.
make sure check the index yourself.
I want to get a character from somewhere inside an NSString. I want the result to be an NSString.
This is the code I use to get a single character at index it:
[[s substringToIndex:i] substringToIndex:1]
Is there a better way to do it?
This will also retrieve a character at index i as an NSString, and you're only using an NSRange struct rather than an extra NSString.
NSString * newString = [s substringWithRange:NSMakeRange(i, 1)];
If you just want to get one character from an a NSString, you can try this.
- (unichar)characterAtIndex:(NSUInteger)index;
Used like so:
NSString *originalString = #"hello";
int index = 2;
NSString *theCharacter = [NSString stringWithFormat:#"%c", [originalString characterAtIndex:index-1]];
//returns "e".
Your suggestion only works for simple characters like ASCII. NSStrings store unicode and if your character is several unichars long then you could end up with gibberish. Use
- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
if you want to determine how many unichars your character is. I use this to step through my strings to determine where the character borders occur.
Being fully unicode able is a bit of work but depends on what languages you use. I see a lot of asian text so most characters spill over from one space and so it's work that I need to do.
NSMutableString *myString=[NSMutableString stringWithFormat:#"Malayalam"];
NSMutableString *revString=#"";
for (int i=0; i<myString.length; i++) {
revString=[NSMutableString stringWithFormat:#"%c%#",[myString characterAtIndex:i],revString];
}
NSLog(#"%#",revString);
I have a UITextField that has data in it separated by commas (i.e, 1,2,4) I want to look at this string and extract all the numbers out of it that are separated by the commas and put it in and array. So in this example 1 2 4 would be stored in an array. Can someone help me code something like this?
Sure, no problem.
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
Now you have an array of NSString objects - something like { #"1", #"2", #"4" }. You can convert those to NSNumbers or to regular integer types if you like. Here's a link to the NSString documentation for your reference.
If there's nothing else in your string of commas and digits, then you can use something like [string componentsSeparatedByString:#","]. Then you'd just need to reiterate over returned array and type-cast each element. If there's some sort of "info-noise" you may want to look into NSScanner class reference.