How to find the next line in the nsstring - ios

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

Related

How do I easily strip just a comma from an NSString?

I have a UILabel with the following text:
Medium, Black
What I intended to do was grab the words in the string and insert each into a mutable array so I could use each title later on to identify something.
Here's how I done this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByString: #" "] mutableCopy];
NSString *size = [chosenOptions objectAtIndex:0];
NSString *colour = [chosenOptions objectAtIndex:1];
I've logged these two NSString and size is returning "Medium," and colour is correctly returning "Black".
My comparison result is always false because of the comma:
itemExists = [[item colour] isEqualToString:colour] && [[item size] isEqualToString:size] ? YES : NO;
That comma causes itemExists to always equal NO.
Would appreciate a simple solution in code please.
The solution needs to only strip commas and not other characters. When dealing with clothing sizes for females I use sizes in a string like this: "[8 UK]" so remove non-alphanumeric characters would remove these. So I really need a solution to deal with just the commas.
Thanks for your time.
Rather than splitting on spaces, you could split on spaces or commas, like this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#" ,"]] mutableCopy];
[chosenOptions removeObject:#""];
This would eliminate commas from the size and colour strings.
[yourString stringByReplacingOccurrencesOfString:#"," withString:#""];
easy squeezy lemon peesey
Try this:
NSString * myString = #"Medium, Black";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#", " withString:#""];
NSLog(#"%#xx",newString);

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

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

add whitespace to string

I have the following string
NSString *word1=#"hitoitatme";
as you can see, if you were to add a space after every second character, it would be a string of words that contain min/max 2 characters.
NSString *word2=#"hi to it at me";
I want to be able to add a white character space to my string after every 2 characters. How would I go about doing this? So if I have a string such as word1, I can add some code to make it look like word2? I am looking if possible for the most efficient way of doing this.
Thank you in advance
There might be different ways to add white space in the string but one way could be using NSRegularExpression
NSString *originalString = #"hitoitatme";
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:#"([a-z])([a-z])" options:0 error:NULL];
NSString *newString = [regexp stringByReplacingMatchesInString:originalString options:0 range:NSMakeRange(0, originalString.length) withTemplate:#"$0 "];
NSLog(#"Changed %#", newString);//hi to it at me
You can do this way:
NSString *word1=#"hitoitatme";
NSMutableString *toBespaced=[NSMutableString new];
for (NSInteger i=0; i<word1.length; i+=2) {
NSString *two=[word1 substringWithRange:NSMakeRange(i, 2)];
[toBespaced appendFormat:#"%# ",two ];
}
NSLog(#"%#",toBespaced);

Resources