How to split strings into substrings in iOS - objective-c

I have a string like this.
NSString *str=#"-85.33,45.78,0.0000 -85.78,46.98,0.000 -85.98,47.678,0.0000";
I have to extract individual strings from the above into an array.
The above string is 3 sets of longitude,latitude,altitude values. these are separated by "," and spaces. I have to extract longitude values into an array and latitude to another array.
Can anyone please suggest a solution.

You could use the componentsSeparatedByString: method of NSString, or you could use the NSScanner class. You could even use componentsSeparatedByString: to split on spaces, and then use NSScanner to parse the numbers out of each substring.

Let me give you a hint. If you have a say a NSString, you can see what methods (with descriptions) are available by command-clicking on NSString in XCode. Then you would have seen componentsSeparatedByString:
The beautiful thing is this works for all objects. At least take a look before asking.

Related

What does stringWithUTF8String do?

So I have done some searching around so that I could see what it was I was doing with my code, and I couldn't find any answers as to what this very one specific line of code does.
NSString* name = [NSString stringWithUTF8String:countryName];
I know what the rest does (I only had to google how to do this part), it is supposed to take my char* (countryName) and turn it into an NSString so later on I can compare it with the
isEqualToString:
thing. I would just like to know what the following is actually doing to the char, and what does the UTF8String even mean?
I have barely any Objective C programming experience so any feedback is helpful :D
you are not totally right.
this method
Returns a string created by copying the data from a given C array of UTF8-encoded bytes.
so, UTF-8 string here is just a C array of bytes.
Check the documentation here.
It doesn't do anything to the char * string. It's just the input to the method. stringWithUTF8String takes a C-style string (in UTF-8 encoding), and creates an NSString using it as a template.

How to add UTF-8 characters to an NSString?

I need to make chemistry formulas (SO4^2-), and the easiest way to make subscripts and superscripts seems to be adding UTF-8 characters, since KCTSuperscriptAttributeName: property of NSAttributedString doesn't work.
Is it possible for me to make an nsstring with normal characters and utf-8 characters?
Thanks
According to NSString Reference "NSString is implemented to represent an array of Unicode characters, in other words, a text string."
It would be convenient to write as below:
NSString* myStr = #"Any Unicode Character You Want";
Just make sure that your default text encoding is unicode.
Justin's answer is good but I think what you might really be looking for is NSAttributedString (documentation linked for you) or NSMutableAttributedString, where you can add superscripts, subscripts, and other character styles that NSString by itself can't handle.
Take a look at other NSAttributedString questions here or via Google, like this potentially related question or this one.
Hope this helps you out!
yes. i assume you know the normal approach to make an NSString - here's one method to create an NSString from a utf8 string: -[NSString initWithUTF8String:].

parse strings separated by 2 elements

On Mac OS X, in Ojective-C, I wanted to know if anyone could give me some pointers as to how I could parse strings contained between "x" and "y".
My current code only enables me to separate the strings separated by one componennt :
NSArray * allLines = [theDataObject componentsSeparatedByString:#"word-1"];
Ideally, I would like to isolate the strings contained between #"word-1" and #"word-2".
Could anyone help please? Thanks in advance!
Check the documentation for NSScanner. The methods you want are -scanUpToString:intoString: and -scanString:intoString:
NSString *str = [theDataObject stringByReplacingOccurencesOfString:#"word-2" withString:#"word-1"];
NSArray *allLines = [str componetnsSeperatedByString:#"word-1"];
You don't care whether its word-1 or word-2 as those do not come back in the array anyways.

to know the character in a object-c nsstring object

I have an NSString *str and I want to know if the 2nd character is equal to a. How can I do this? I'm new to objective c.
Use this:
[str characterAtIndex:1]
But I always encourage you to check documentation first!
You could also use:
[[str substringWithRange:NSMakeRange(1,1)] isEqualToString:#"a"];
Admittedly, this only becomes handy when you have longer strings for which you're searching.

Help in padding numerical strings

I am trying to generate a numerical string by padding the number with zeroes to the left.
0 would become 00000
1 would become 00001
10 would become 00010
I want to create five character NSString by padding the number with zeroes.
I read this Create NSString by repeating another string a given number of times but the output is an NSMutableString.
How can I implement this algorithm with the output as an NSString?
Best regards.
You can accomplish this by calling
[NSString stringWithFormat:#"%05d", [theNumber intValue]];
where theNumber is the NSString containing the number you want to format.
For further reading, you may want to look at Apple's string formatting guide or the Wikipedia entry for printf.
One quick & simple way to do it:
unsigned int num = 10; // example value
NSString *immutable = [NSString stringWithFormat:#"%.5u", num];
If you actually really want to use the long-winded approach from the example you read, you can send a “copy” message to a mutable string to get an immutable copy. This holds for all mutable types.