obtaining certain characters from a string by position - iphone

NSString *str = #"ABCDEFGHI";
I would like to get the characters into position.
For example, i would extract the first, third and fourth position character: "ACD"

Look up -substringWithRange:

NSString *trimmedString=[str substringFromIndex:[str length]1];
NSString *trimmedString1=[str substringFromIndex:[str length]3];
NSString *trimmedString2=[str substringFromIndex:[str length]4];
and append them to get desired result.

Related

stringWithFormat and % not working

i have this string
NSString *jsonString = #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D";
NSLog(#"%#",jsonString);
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D
when i use
NSString *linkId = #"448";//not a constant value only for example
NSString *jsonString = [NSString stringWithFormat:#"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A%#%7D",linkId];
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=7 37040ate23A222㿠 37040isplay23A0x1.21800000507cp-1027ll27D&action=showMatches&params=7 –ompetition_id23A(null) 0
as you see not the same.My question is how to use stringWithFormat to get this result:
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A448%7D
so the value (721) just at the and is replaced by (448)
thanks in advance.
It's because all those % characters inside your format string are being potentially used to used the format arguments, much like %# (see here for details).
This can be seen (for one instance) where:
callback_params=%7B%22date
is transformed into:
callback_params=7 37040ate
In that case, I'm not sure what the %7B is doing since it's not a valid format specifier, but the %22date is resulting in a 22-character decimal value, from %22d, followed by the literal ate.
You need to use %% in your format string if you want a single % in the output string.
The other way of looking at it is that the thing you're giving it as a format string is really data, not purely a format.
To be safe from those spurious conversions, you'd want:
NSString *jsonString = [NSString stringWithFormat:#"%#%#%#", #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A",linkId, #"%7D"];

Find words with regEx and then add whitespaces inbetween with Objective-c

I was wondering how to add whitespaces inbetween letters/numbers in a string with Objective-C.
I have the sample code kinda working at the moment. Basically I want to turn "West4thStreet" into "West 4th Street".
NSString *myText2 = #"West4thStreet";
NSString *regexString2 = #"([a-z.-][^a-z .-])";
for(NSString *match2 in [myText2 componentsMatchedByRegex:regexString2 capture:1L]) {
NSString *myString = [myText2 stringByReplacingOccurrencesOfString:match2 withString:#" "];
NSLog(#"Prints out: %#",myString); // Prints out: Wes thStreet // Prints out: West4t treet
}
So in this example, it's replacing what I found in regEx (the "t4" and "hS") with spaces. But I just want to add a space inbetween the letters to separate out the words.
Thanks!
If you wrap parts of your regex patterns in parentheses, you can refer to them as $1, $2, etc in your replacement string (patterns are numbered from left to right, by the order of their opening parenthesis).
NSString *origString = #"West4thStreet";
NSString *newString = [origString stringByReplacingOccurrencesOfRegex:#"(4th)" withString:#" $1 "];
Not sure I understand your broader use case, but that should at least get you going...

Splitting a number off prefix of a string on iPhone

Say I have a string like "123alpha". I can use NSNumber to get the 123 out, but how can I determine the part of the string that NSNumber didn't use?
You can use NSScanner to both get the value and the rest of the string.
NSString *input = #"123alpha";
NSScanner *scanner = [NSScanner scannerWithString:input];
float number;
[scanner scanFloat:&number];
NSString *rest = [input substringFromIndex:[scanner scanLocation]];
If it is important to know exactly what is left after parsing the value this is a better approach than trying to trim characters. While I can't think of any particular bad input at the moment that would fail the solution suggested by the OP in the comment to this answer, it looks like a bug waiting to happen.
if your numbers are always at the beginning or end of a string and you want only the remaining characters, you could trim with a character set.
NSString *alpha = #"123alpha";
NSString *stripped = [alpha stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]];
If its starts out as a char * (as opposed to an NSString *), you can use strtol() to get the number and discover where the number ends in a single call.

How do you split NSString into component parts?

In Xcode, if I have an NSString containing a number, ie #"12345", how do I split it into an array representing component parts, ie "1", "2", "3", "4", "5"... There is a componentsSeparatedByString on the NSString object, but in this case there is no delimiter...
There is a ready member function of NSString for doing that:
NSString* foo = #"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:#"/"];
It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.
A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.
A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.
If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.
In your case, since you have no delimiter, you have to get separate chars by
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
or this one
- (unichar)characterAtIndex:(NSUInteger) index inside a loop.
That the only way I see, at the moment.
Don't know if this works for what you want to do but:
const char *foo = [myString UTF8String]
char third_character = foo[2];
Make sure to read the docs on UTF8String

Replace occurrences of NSString - iPhone

I have a long NSString in which I m trying to replace special characters. Part of my string looks like this:
"veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)"
I would like to replace all the \u0153 with "oe". I've tried:
[response stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];
but it doesn't work.... I don't understand why!
The backslash is an escape character, so if you want to specify the actual backslash character in a string literal, you need to use two backslashes.
NSString *new = [old stringByReplacingOccurrencesOfString: #"\\u0153" withString:#"oe"];
NSString is immutable, so the function generates a new string that you have to store:
NSString *new = [old stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];