Convert integer to string and use each character - objective-c

How is an integer converted to a string in objective c and how is each character of the string reached?
The equivalent to the java charAt() method.

NSString *myString = [NSString stringWithFormat:#"%i", 36];
[myString characterAtIndex:0]; //"3"
[myString characterAtIndex:1]; //"6"
Update: Note that characterAtIndex returns a unichar not a c char. Thanks #Chuck.

Related

How to Trim special Characters in a String in Objective c

I want to trim the occurence of special characters in a string
String has :
prevs: Case Number
____________________
In this i want to remove -------- this dash from this string .I have tried like this :
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:#"-"];
NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:#""];
Thanks in Advance!
Try This:
NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSString *trimedString = [myString stringByReplacingOccurrencesOfString:#"-" withString:#""];
Use regular expression, the pattern [\\s_]{4,} searches for 4 and more whitespace or underscore characters.
NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:#"[\\s_]{4,}" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];
If the underscore characters are really dashes reaplace the character in the pattern.
You can use below code to remove your special string.
NSString *yourString = #"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:#""];
NSLog (#"Your final string : %#", finalString);

I want to read a string of type "t = 10" in Obj C, and take the value of the character and the value of the integer. What is the best way to do that?

I want to read a string of type "t = 10" in Obj C, and take the value of the character and the value of the integer. What is the best way to do that?
You can use NSScanner for that:
NSScanner *scn = [NSScanner scannerWithString:#"t = 10"];
NSString *theChar;
[scn scanUpToString:#" = " intoString:&theChar];
[scn scanString:#" = " intoString:NULL];
int n;
[scn scanInt:&n];
here theChar will contain an NSString object containing the char and n will contain the numerical value of the integer.
Take a look at strtok, which is part of the C standard library.
It's slightly complicated, but should do a good portion of your work for you.
The "t", "=", and "10" are what the docs refer to as "tokens", whereas the whitespace is the "separator".
If you don't care about the "=", you could have strtok treat that as a separator, too.
you can convert your string to NSArray
// separate the string to array by the = sign
NSArray *myArray = [myString componentsSeparatedByString:#"="]
// get the character
NSString *character = [myArray ObjectAtIndex:0];
// remove the whitespaces from the character
character = [character stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
// get the value
NSString *value= [myArray ObjectAtIndex:1];
// remove the whitespaces from the value
value= [value stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
hope it helps :)

Objective-C ASCII decimal integer to NSString conversion

How can I convert and ASCII decimal integer to an NSString in Objective-C? (Example: 36 to "$")
Thanks
Try this:
unichar asciiChar = 36;
NSString *stringWithAsciiChar = [NSString stringWithCharacters:&asciiChar length:1];
You can use the following:
NSString *s = [NSString stringWithFormat:#"%c", 36];
NSLog(#"%#", s); // $

cut a char in a string

I have a string
string = "01";
but I want to delete the '0' and have a new string with only '1'. Is there a fast solution?
-(NSString *) substringFromIndex: i
Returns a substring from the character at i to the end
-(NSString *) substringWithRange: range
Returns a substring based on a specified range
-(NSString *) substringToIndex: i
Returns a substring from the start of the string up to the character at index i
And if you only want to remove 0's before any nonzero value then make a
int i = [str intValue];
str = [NSString stringWithFormat:#"%d",i];
You can use this:-
NSString *string=#"01";
NSString *temp=[string substringWithRange:NSMakeRange(1, 1)];
NSLog(#"temp%#",temp);
If you want to remove the leading zero from an arbitrary length NSString object you could do:
NSRange aRange;
aRange.location = 0;
aRange.length = 1;
[theBreakerCode stringByReplacingOccurrencesOfString:#"0" withString:#"" options:NSLiteralSearch range:aRange];
Strings without a leading zero are untouched.

Get a string with ascii code objective-c

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