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.
Related
Sorry for the rather simple question but I'm pretty new to objective-c and can't seem to find a solution to my problem that actually works with what i'm trying to do.
Essentially, I have a NSString containing the a random word.
For example:
NSString *word = #"Characters";
I then want to take this and split it into an array consisting of the characters so that I can index each of them:
[0] C
[1] H
[2] A
etc
It's not really important if after they're split if they're put into a NSString or an Array, as long as I can separate the string into each off the individual characters then index them.
Also for clarification, the word isn't hard coded it's randomly generated every time my program runs and pulled from a word list.
Any help is appreciated!
There's no reason to do this. Internally, the string already is an array of characters, and you can access the individual characters by index...
[word characterAtIndex:2] // returns 'a'
Update Swift 4+
Default you have a string
let string = "Characters"
so, all you need to do is...
for i:Int in 0..< string.count {
let char = NSString(format: "%c", (string as NSString).character(at: i))
print(char) //Optionally you can convert using 'char as String'
}
Note:- this is the update for this answer
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.
I have three strings (a NSString, a NSMutableString, and another NSString) which I need to concatenate into a mutable string, in that order, to display as the source for a UIWebView. Comming from a PHP/JavaScript/HTML background, my knowledge of concatenation is pretty much this:
var concatenatedString = string1 + string2 + string3;
I presume that sort of thing won't work in Objective-C, so I'm wondering how to go about pulling them all together properly.
To give a bit of setting for this, the first string (NSString) is the header and canvas element of a web page, the second string (NSMutableString) is javascript from a text field that the user can define to manipulate the canvas element, and the third string (NSString) is the end tags of the web page.
Also, rather than initially creating the NSMutableString, should I just referance the UITextView.text to the get the user's text when concatenating the whole thing, or should I pull the text from the UITextView first?
NSMutableString *concatenatedString = [[NSString stringWithFormat:#"%#%#%#", string1, string2, string3] mutableCopy];
The other two answers are correct in that they answer the question as you asked it. But by your description of what you want to do there is a much easier way. Use a format.
Assuming string1 and string3 will always be the same and only string2 will change,which is what it sounds like you are doing you can write something like this.
static NSString *formatString = #"String1Text%#String3Text";
NSString *completeString = [NSString stringWithFormat:formatString,self.myTextFieldName.text];
NSLog(#"%#",completeString);
The %# in the format says to insert the description of the object following the format.(The description of an NSString is the stringValue.)
Assuming you have a UITextField named myTextFieldName, that currently contains the text 'String2Text' Then this will be the output:
'String1TextString2TextString3Text'
In this way you only create 1 instance of an NSString format for the whole class no matter how many times you call this code.
To me it sounds like you don't need a mutable string at all. Feel free to leave a comment if I misunderstood anything.
Response to comment:
I'm not sure how you are implementing 'moves to test it out again' but, let's say you have a button named 'testJavaScript'. The IBAction method connected to that button would have the first two lines in it. So each time you pushed the button it would make a new formatted NSString filled with the current contents of the textfield. Once this string was formed it could not be changed. But it won't matter since next time it will make another.
NSString *concatenatedString = [string1 stringByAppendingFormat:#"%#%#", string2, string3];
You can make the resulting string mutable (if you really need to) by adding mutableCopy as shown in the answer by #Vinnie.
I am trying to replace only one of the occurrences of a NSString with another string. I know that you can use stringByReplacing*Occurrences*OfString; however, that replaces all occurrences. What I am looking for is more like stringByReplacing*Occurrence*OfString. If anyone could help me that would be great. Thanks,
Jnani
Something like this?
NSRange location = [someString rangeOfString:stringToReplace];
NSString* result = [someString stringByReplacingCharactersInRange:location withString:stringToReplaceWith];
I've been trying to read sections of a string into an array and encountered some problems. Though my task is more complicated, even a simple example case like the one below gives the same problem: the code compiles and runs but the the size of the strings array is always one and the only thing that's contained in strings[0] is "__NSArrayM".
NSString *string = #"John, Bob, Jane";
NSArray *strings = [string componentsSeparatedByString: #", "];
Thanks in advance for any ideas!
When you say "the size of the strings array", do you mean what you get from [strings count] or something else? Also, Objective-C doesn't let you access NSArray elements with the square bracket notation, so if you're literally calling strings[0] you're doing some pointer math on the Array object that you don't intend to be doing.