String compare in iOS - ios

I want to find out whether a given NSString or CFStringRef contains a certain substring. How do I do that?

- (NSRange)rangeOfString:(NSString *)aString
...is your man. For example:
NSString* search = #"Where's the manual?";
NSRange range = [search rangeOfString: #"the"];
NSLog( #"Found at %u", range.location );

Please read the NSString class reference. What you want is pathExtension.

Related

how to capture a single character from NSString using substringToIndex

I have a NSString that has two characters.
The NSSring looks something like this
FW
I use this code to capture the first character
NSString *firstStateString = [totInstStateString substringToIndex:1];
this pecie of code returns F, I would like to know how to return the second character to its own string using substringToIndex.
anyhelp would be appreciated.
Use substringWithRange:
For Example :
[#"fw" substringWithRange:NSMakeRange(1, 1)];
will get you #"w"
have a look at the apple docs
Use following code .. My be helpful in your case.
NSString *myString = #"FFNNF";
for(int i = 0; i < [myString length]; i++)
{
NSLog(#"%#", [myString substringWithRange:NSMakeRange(i, 1)])
}
Also check This Question.
How to get a single NSString character from an NSString
The other option is:
NSString *lastStateString = [totInstStateString substringFromIndex:1];
This will get the last character of a two-character string. You might want to do some bounds checking somewhere in there.

iOS: changing NSString value

Will this bit of code produce any memory leaks? Is it the correct way to change NSString values?
NSString * enemiesAndElementsTextureFileName = #"bla bla";
enemiesAndElementsTextureFileName = #"bl";
That way of doing it won't cause any memory leaks and it is indeed correct. In this case you wouldn't need an NSMutableString because you aren't altering the string literal itself, you are simply replacing the string value with a new one (replacing #"bla bla" with #"bl").
In this case, however, your string will now be 'bl', so you can delete that first line value and just have NSString * enemiesAndElementsTextureFileName = #"bl";
Yes NSString allocated once. This is one of the way
Yes, use NSMutableString with the following method as your needs:
// Allocate
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
// set string content
[str setString:#"1234"];
// Append
[str appendString:#"567"];
// Concat
[str appendFormat:#"age is %i and height is %.2f", 27, 1.55f];
// Replace
NSRange range = [str rangeOfString:#"height"];//查找字符串height的位置
[str replaceCharactersInRange:range withString:#"no"];
// Insert
[str insertString:#"abc" atIndex:2];
// Delete
range = [str rangeOfString:#"age"];
[str deleteCharactersInRange:range];
NSLog(#"%#", str);

iOS finding string within a string

Hello everyone I am trying find a string inside a string
lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/"
so what I will get from that string is:
Word3
How do I do that?
Thanks!
You are looking for the componentsSeparatedByString: method
NSString *originalString = #"word1/word2/word3";
NSArray *separatedArray = [originalString componentsSeparatedByString:#"/"];
NSString *lastObject = [separatedArray lastObject]; //word3
once check this one By using this one you'l get last pathcomponent values,
NSString* theFileName = #"how /are / you ";
NSString *str1=[theFileName lastPathComponent];
NSLog(#"%#",str1);
By using lastPathComponent you'l get the last path component directly no need to take array for separate the string.
you must use NSScanner class to split substring.
check this.
Objective C: How to extract part of a String (e.g. start with '#')
NSString *string = #"word1/word2/word3"
NSArray *arr = [string componentsSeperatedByString:#"/"];
NSSting *str = [arr lastObject];
You can find it also with this way:
NSMutableString *string=[NSMutableString stringWithString:#"word1/word2/word3"];
NSRange range=[string rangeOfString:#"/" options:NSBackwardsSearch];
NSString *subString=[string substringFromIndex:range.location+1];
NSRegularExpression or NSString rangeOfString:options:range:locale: (with options to search backwards).
The answer really depends on exactly what the input string will contain (how consistent it is).

Cut NSString from space to end

I have NSString:
sessid=os3vainreuru2hank3; __ubic1=MzcxMzjMDYuNjk0NDA1Mzc%3D;
auto_login=123; sid=kep8efpo7; last_user=123;
I need get just:
__ubic1=MzcxMzjMDYuNjk0NDA1Mzc%3D; auto_login=123;
interpals_sessid=kep8efpo7; last_user=123;
But count of characters past sessid may vary
Thanks! Sorry for simple question
This should do the trick.-
NSRange range = [yourString rangeOfString:#" "];
if (NSNotFound != range.location) {
yourString = [yourString substringFromIndex:(range.location + 1)];
}
Basically, you get the index for the first space character, and then the substring from that index to the end.
You'll need at least one character you can search for. Looks like that double underscore will work.
NSRange stringStart = [originalString rangeOfString:#"__"];
NSString *extractedString = [originalString substringWithRange:NSMakeRange(stringStart.location, originalString.length - stringStart.location)];
That should get you what you need!
NSMutableArray* array = [[originalString componentsSeperatedByString:#";"] mutableCopy];
[array removeObjectAtIndex:0];
NSString* newString = [array componentsJoinedByString:#";"];
I assume you mistyped interpals_sessid with sid

Split NSString and Limit the response

I have a string Hello-World-Test, I want to split this string by the first dash only.
String 1:Hello
String 2:World-Test
What is the best way to do this? What I am doing right now is use componentsSeparatedByString, get the first object in the array and set it as String 1 then perform substring using the length of String 1 as the start index.
Thanks!
I added a category on NSString to split on the first occurrence of a given string. It may not be ideal to return the results in an array, but otherwise it seems fine. It just uses the NSString method rangeOfString:, which takes an NSString(B) and returns an NSRange showing where that string(B) is located.
#interface NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString;
#end
#implementation NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString
{
NSRange range = [self rangeOfString:splitString];
if (range.location == NSNotFound) {
return nil;
} else {
NSLog(#"%li",range.location);
NSLog(#"%li",range.length);
NSString *string1 = [self substringToIndex:range.location];
NSString *string2 = [self substringFromIndex:range.location+range.length];
NSLog(#"String1 = %#",string1);
NSLog(#"String2 = %#",string2);
return #[string1, string2];
}
}
#end
Use rangeOfString to find if split string exits and then use substringWithRange to create new string on bases of NSRange.
For Example :
NSString *strMain = #"Hello-World-Test";
NSRange match = [strMain rangeOfString:#"-"];
if(match.location != NSNotFound)
{
NSString *str1 = [strMain substringWithRange: NSMakeRange (0, match.location)];
NSLog(#"%#",str1);
NSString *str2 = [strMain substringWithRange: NSMakeRange (match.location+match.length,(strMain.length-match.location)-match.length)];
NSLog(#"%#",str2);
}

Resources