Is there any way to split up a NSString to an array with all the letters separated?
Something like:
NSString *str = #"hey dude";
//Array output : h,e,y,d,u,d,e
[stringToCopy getCString:c_buffer maxLength:c_buffer_length encoding:NSUTF8StringEncoding];
Related
I have a NSMutableString variable that i append from aString, like this:
NSString *aString = [NSString stringWithFormat:#"{\"MyCode\":%#,\"TotalAmount\":%#,\"PaymentType\":\"%#\",\"BankName\":\"%#\",\"BankAccountNo\":\"%#\",\"Amount\":\"%#\",\"FileName\":\"%#\"}",aCode,total,type,labelBank.txt,labelAcc.txt,aTrasfer,imageName];
[teststring appendString:asstring2];
[teststring appendString:#","];
in this code i sucess to append the string in order they append. But know i want to append a new string in the first position, just like in array object at index 0.Can NSMutableString do this?
Hope my question is clear..thanks
You said it, with an index. Same way as you do with an array.
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
Your code:
[testString appendString:aString];
[testString insertString:#"," atIndex:0]; /* prepend the comma
Check out the Documentation
I have this string:
Test: String
And I want to select the text before the :, as if that were the result: "Test";
Any tip?
Thanks,
Alberto
You can split the string:
NSArray *words = [string componentsSeparatedByString:#":"];
Then select array's first element.
NSString *select = [words objectAtIndex:0];
NSLog(select);
NSString *firstWord = [[string componentsSeparatedByString:#":"] objectAtIndex:0];
you can use
NSString *str = #"Test: String";
NSSArray *Arrstr = [str componentsSeparatedByCharactersInSet:#":"];
Now you will have two strings in the array. Take first by :
NSString *str1 = [Arrstr objectatindex : 0];
Hope that helps you.
I have a set of NSString representing the names of the files in a directory. These names are structured this way:
XXXXXXXXX_YYYY_AAAA.ext
All the sections separated by "_" are of variable length and I would only have the first.
How can I separate the first part from the other?
Find the position of the '_' character, then get a substring 0 through that position. Note that substringToIndex: does not include the character at the index position.
NSRange r = [myString rangeOfString:#"_"];
NSString *res = [myString substringToIndex:r.location];
Take a look at the NSString method componentsSeparatedByString:. That will tokenize a string and return you an array. Something like this:
NSArray *array = [#"XXXXXXXXX_YYYY_AAAA.ext" componentsSeparatedByString:#"_"];
NSString *firstToken = [array objectAtIndex:0];
NSArray *array = [yourString componentsSeparatedByString:#"_"];
NSString *Xs = [array objectAtIndex:0];
Try componentsSeparatedByString: under the heading Dividing Strings.
NSString Docs
I have a ascii code, for the letter 'a', and I want to get a string by its ascii code, is it possible with NSString?
This could also work:
NSString *foo = [NSString stringWithFormat:#"%c", 97];
Didn’t test it.
If you mean you have a byte that represents an ASCII-encoded character and you want to make a string out of it, NSString has an initializer just for that.
char characterCodeInASCII = 97;
NSString *stringWithAInIt = [[NSString alloc] initWithBytes:&characterCodeInASCII length:1 encoding:NSASCIIStringEncoding];
I have multiple NSStrings and i wish to merge them into one other, here is my code so far...
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = #"Hey!<br>I just snipped my long url with My Cool App for iPhone in just a few seconds!<p><b>"+newURL+#"</b></p>";
If you know the number of your existing strings, you can just concat them:
NSString* longString = [firstString stringByAppendingString:secondString];
or:
NSString* longString = [NSString stringWithFormat:#"A string: %#, a float: %1.2f", #"string", 31415.9265];
If you have an arbitrary number of strings, you could put them in an NSArray and join them with:
NSArray* chunks = ... get an array, say by splitting it;
NSString* string = [chunks componentsJoinedByString: #" :-) "];
(Taken from http://borkware.com/quickies/one?topic=NSString)
Another good resource for string handling in Cocoa is: "String Programming Guide"
You can try
NSString *emailBody = [ NSString stringWithFormat: #"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newURL ];
Given that you've got multiple strings I recommend using an Array:
NSArray *array = [NSArray arrayWithObjects:#"URL", #"person", "body"];
NSString *combined = [array componentsJoinedByString:#""];
Formatting string has better readability and less error-prone:
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = [NSString stringWithFormat:#"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newUrl, newUrl];
You can concatenate strings in Cocoa using:
[NSString stringByAppendingString:]
Or you could use the [NSString stringWithFormat] method which will allow you to specify a C-style format string with a variable argument list to populate the escape sequences.