How do you assign characterAtIndex to NSString variable? [closed] - objective-c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've looked at several posts to answer my question but i keep getting errors.
I basically have an NSString that holds 2 characters...
myString = #"ABC";
All I want to do is assign the first character in myString to another NSString variable...
NSString *firstChar;
firstChar = [myString characterAtIndex:0];
I get this error when i do this:
Incompatible integer to pointer conversion assigning to 'unichar *' (aka 'unsigned short *') from 'unichar' (aka 'unsigned short')
What am I doing wrong?

NSString's characterAtIndex: returns a unichar, not a string, so you can't just assign it to another string. You need to create a string from it first:
NSString *myString = #"ABC";
unichar firstChar = [myString characterAtIndex:0];
NSString *string = [NSString stringWithCharacters:&firstChar length:1];
Of course, as DarkDust says, you can also use substringWithRange: to get a string from a subrange of a string:
[myString subStringWithRange:NSMakeRange(0, 1)]; // to get the first character
[myString subStringWithRange:NSMakeRange(1, 2)]; // to get the second and third characters

The characterAtIndex: method returns a unichar (an integer representation of the character). What you want is -[NSString substringWithRange:], like [myString substringWithRange:NSMakeRange(0, 1)];

Related

Objective C remove end of string after certain character [duplicate]

This question already has answers here:
Remove Characters and Everything After from String
(2 answers)
Closed 8 years ago.
I can't seem to find the answer to this anywhere. I can do it in c but objective c is difficult.
I want to cut the end of a string after a certain character
so user#example.com will become user (cut at '#')
How do I do this?
This will give you the first chunk of text that comes before your special character.
NSString *separatorString = #"#";
NSString *myString = #"user#example.com";
NSString *myNewString = [myString componentsSeparatedByString:separatorString].firstObject;
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:
You can use a combination of substringToIndex: and rangeOfString: methods, like this:
NSString *str = #"user#example.com";
NSRange pos = [str rangeOfString:#"#"];
if (pos.location != NSNotFound) {
NSString *prefix = [str substringToIndex:pos.location];
}
Notes:
You need to check the location against NSNotFound to ensure that the position is valid.
substringToIndex: excludes the index itself, so the # character would not be included.

Convert NSUInteger to string with ARC [duplicate]

This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

clean a string extracting all the non numeric characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm quite new in Objective C. I'm trying to convert the following string 5896.3454A into a numeric string 58963454. Any idea how to do it.
I have been trying with NSScanner class, but I'm not sure, whether this is the best approach.
One possible solution would be to use a string replacement with regular expressions:
NSString *mixed = #"5896.3454A";
NSString *pattern = #"\\D"; // Pattern for "not a digit"
NSString *digitsOnly = [mixed stringByReplacingOccurrencesOfString:pattern
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, [mixed length])];
Objective-C contains a lot of little tricks. Here is a silly one to solve your problem.
NSString *string = #"5896.3454A";
NSCharacterSet *digits = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSCharacterSet *nonDigits = [digits invertedSet];
NSArray *digitSubstrings = [componentsSeparatedByCharactersInSet:nonDigits];
NSString *result = [digitSubstrings componentsJoinedByString:#""];
result will have only the digits in string.
I create a character set with all the digits. I use invertedSet to get a set with every character other than the digits. This is straight forward, next is the silly part. Next I split the string into an array of substring. This will have the effect of stripping out all the non digit characters, but all the digits are broken up into substrings. In this case, digitSubstrings will be #[#"5896", #"3454", #""]. Not a problem, I join all the substrings back together to create the final string.
You can do it also by
yourString mentioned in your question
NSInteger newInt = [ [ [ yourString stringByReplacingOccurrencesOfString:#"." withString:#""] stringByReplacingOccurrencesOfString:#"A" withString:#""] integerValue];
ADDED: NSInteger newInt = [ [ #"123.123A" stringByReplacingOccurrencesOfString:#"." withString:#""] integerValue]; combining NSString and NSScanner methods, integerValue ignores everything beginning with the first appearance of a character
You find it in the NSString class. Whether this is the better solution depends on the unwanted literals in your "string-number" you want to replace/delete.

Objective C Regular Expression removing everything except numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove all character, the things that I need is number from 0 - 9
Could you please show me some example to solve this?
You could make your own NSCharacterSet and then trim out every character not contained within it by using its invertedSet. Here's an example:
NSString *stringWithLettersAndNumbers = #"345345345fff";
NSCharacterSet *myCharset = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSString *newString = [stringWithLettersAndNumbers stringByTrimmingCharactersInSet:[myCharset invertedSet]];
NSLog(#"%#",newString);
Regular expressions are great for this sort of stuff:
NSString *orgStr = #"letters34243more32132letters";
NSString *result = [orgStr stringByReplacingOccurrencesOfString: #"\\D"
withString: #""
options: NSRegularExpressionSearch
range: NSMakeRange(0, orgStr.length)];
What it does is find any non-digit character (defined as \D) and replace it with an empty string, i.e., remove it.
It does it for any such character, irrespectively of its position in the string. In my reading of the question, this is what you need.
The double backslash ("\") in the pattern string is necessary because NSString literals need an extra backslash to escape the "real" one.
Loop through the string and pickup all digits.
NSMutableString *strResult = [NSMutableString string];
for (int position=0; position<text.length; position++) {
unichar c = [text characterAtIndex:position];
NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
if ([numericSet characterIsMember:c]) {
[strResult appendFormat:#"%C", c];
}
}

How to get the substring of a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get substring of nsstring?
how do I get a substring in iOS?
I am new to iphone.In my project i have a string such as chapter1 but actually i want the substring 1 only how it is possible.if any body know this please help me
Check out the substringFromIndex: or substringToIndex: methods of NSString. You can find the documentation for NSString here.
Ex:
NSString *string = #"Chapter1";
NSString *newString = [string subStringFromIndex: ([string length] - 1)]; // => "1"