Searching for a specific last character in a string - objective-c

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}

Related

How to iterate through a string and find a certain character?

I want to go through what the user input. The user is going to input some 'useless' information that i don't need right now. How would i get the slice the string so I can get the part I want.
for example:
NSString *findingText = [prefs stringForKey:userSearching];
NSString *substring = [string substringFromIndex:[string length] + 4];
But this is where i have put a specific character that i would like to search for which is !. The user has entered some information that I have used as the key. So when the user wants to find the info all they have to do is search for it and will find the special character and use that to get the NSUserDefaults. but what the User Enters doesn't always have the same length. so was wondering how I could do this.
Thanks in advance.
You can use rangeOfString function
NSRagne range = [string rangeOfString:#"!"];
This will give you an NSRange struct that gives you the position of the ! character.
NSUInteger pos = range.location;

Search exact word in NSString

I need to find a word or several words. With this method, however, I find also piece of word.
NSString *searchString = [NSString stringWithFormat:#"%#",searchField.text];
NSRange range = [textString rangeOfString : searchString];
if (range.location != NSNotFound) {
NSLog(#"textString = %#", textString);
}
I need the word / words exact
How can I do?
Thank you!
There are various ways of parsing/finding sub-strings in NSString:
NSString itself
NSRegularExpression. This would probably better suit your needs since you can tackle the scenario of surrounding white-spaces around words. Thus is won't return the cat from catapult when searching for cat.
NSScanner (most likely overkill for you needs)
... and they, of course, each have their PROs and CONs.
NSString has 9 methods grouped under "Finding Characters and Substrings". Methods such as:
-rangeOfString:
Finds and returns the range of the first occurrence of a given string within the receiver.
NSRegularExpression has 5 methods grouped under "Searching Strings Using Regular Expressions". Methods such as:
-numberOfMatchesInString: options: range:
Returns the number of matches of the regular expression within the specified range of the string.
It might also be useful to know about NSScanner, but this class would be more useful if you're parsing the string than simply looking for sub-parts.
What happens if you add a space at the end of the search string, like so:
NSString *searchString = [NSString stringWithFormat:#"%# ",searchField.text];
If the string from searchField.text already ends with a space, you would have to remove it.
This is not a perfect solution yet, for example you would not find the search string if it is at the end of a sentence. Instead what you could do is not adding the whitespace character, but instead look at the character after the hit and make sure that it is not a letter. For this, take a look at the class NSCharacterSet:
NSCharacterSet * letters = [NSCharacterSet letterCharacterSet];
if (![letters characterIsMember:[textString characterAtIndex:(range.location+searchString.length)]]) {
...
}

How to get a single NSString character from an NSString

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);

Extracting Numbers out of a String and put it in an array objective C

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.

Extract values between parenthesis from NSString

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