iOS get substring from a string? - ios

I'v a string like - NSString * str = #"a. System discharges to the ground or to surface waters\n\nb. System causes sewage backup in structure\n\nc. “Black Soil” above system or drain field\n\n\nd. Ponding or puddles around tank, distribution boxes, or drain field" and want some specific substring from str for e.g. b. System causes sewage backup in structure
i have tried
NSRange r1 = [str rangeOfString:#"b. "];
NSString* substr = [str substringFromIndex:r1.location];
NSString* s1 = [substr stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
but i got whole string starting from b. System....
What i'm doing wrong?

Try something like:
NSRange r1 = [str rangeOfString:#"b. "];
NSString *substr = [str substringFromIndex:r1.location];
NSRange r2 = [substr rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
NSString *s1 = [substr substringToIndex:r2.location];

You have your starting point right but you also need to make it right at the end point. This code might help you. Though this mayn't be the exact thing that you might be looking for, it will surely help you in getting the desired result.
NSString *badStr = [NSString stringWithUTF8String:[response bytes]];
NSString *goodStr = [badStr substringFromIndex:76];
NSString *finalStr = [goodStr substringToIndex:[goodStr length]-9];
Basically, this code removes the unwanted characters from the beginning as well as from the ending of the string.
Hope this helps.

Related

Removing Specific Characters from NSString

For example if i had a string like
NSString *myString = #"A B C D E F G";
and I want to remove the spaces, and get a string out like "ABCDEFG".
I could use
NSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];
However, for my application I am loading in phone numbers from the address book
and the numbers often have a different formatting layout.
I'm wondering if I have a phone number stored in a string like
+1-(937)673-3451 how would I go about removing only the first "1" the "+" the "-" and the "(" ")".
Overall, I would like to know if it is possible to remove the first "1" without removing the last one in the string?
There are a lot of ways to do this. Here's one:
NSString *phoneNumber = #"+1-(937)673-3451";
NSCharacterSet *removalCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+()-"];
NSArray *components = [phoneNumber componentsSeparatedByCharactersInSet:removalCharacterSet];
NSString *simplifiedPhoneNumber = [components componentsJoinedByString:#""];
NSRange firstCharacterRange = NSMakeRange(0, 1);
if ([[simplifiedPhoneNumber substringWithRange:firstCharacterRange] isEqualToString:#"1"]) {
simplifiedPhoneNumber = [simplifiedPhoneNumber stringByReplacingCharactersInRange:firstCharacterRange withString:#""];
}
NSLog(#"Full phone number: %#", phoneNumber);
NSLog(#"Simplified phone number: %#", simplifiedPhoneNumber);
But really you want to use a library that knows what a phone number is supposed to look like, like libPhoneNumber.

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

How to find the next line in the nsstring

Is there any way to find the next line characters in the nsstring. I mean second line of characters...
I want to find the word in the second line of nsstring... Plz help me out guys...Am newbie to xcode...
Am using
NSString *substring = [text substringToIndex:[text rangeOfString:#" "].location];
i want to find out that empty space in the entire text... I'm able to find that empty space in the first line of nsstring.. But in the second line also having same empty space.. but it is not recognising...
You can split on \n character.
Something like this:
NSArray *vals = [yourString componentsSeparatedByString:#"\n"];
Then check if vals has more than one element.
if ([vals count] > 1) {
// you do have a "next line"
NSString *nextLine = [vals objectAtIndex: 1];
NSLog(nextLine);
}
NSArray *lines = [yourNSStringObject componentsSeparatedByString:#"\n"];
NSString *secondline = [lines objectAtIndex:1];
There's an overload method for rangeOfString that you can provide a range for it.
Try this:
NSString *substring = [text substringToIndex:[text rangeOfString:#" " options:nil range:NSMakeRange([text rangeOfString:#"\n"], text.length)].location];

Categories

Resources