Objective C Regular Expression removing everything except numbers [closed] - objective-c

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

Related

Delete whiteSpaces in Objective C [duplicate]

This question already has an answer here:
How do you remove extra empty space in NSString?
(1 answer)
Closed 6 years ago.
I am trying to delete the extra white spaces in my string
for exemple
NSString *mystring= #" Alex mona ok";
so after deleting the extra white spaces mastering should look like this
// deleting the first spaces, middle spaces and the last spaces
"Alex mona ok"
Unfortunately, Cocoa's split method is not versatile enough to remove duplicate separators on its own, so you need to write quite a bit of code:
Split your string into words on whitespace
Remove empty entries created for adjacent separators
Join the array back on a single space
Here is the same thing coded in Objective-C:
NSString *mystring= #" Alex mona ok";
NSMutableArray *words = [[mystring componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
[words removeObject:#""];
NSString *res = [words componentsJoinedByString:#" "];
If you only need to remove a certain character like a space use this:
[mystring stringByReplacingOccurrencesOfString:#" " withString:#""]
If you need to remove tabs, spaces, etc. use:
NSArray* newstring = [mystring componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* nospacestring = [newstring componentsJoinedByString:#" "];
This removes all whitespace and then joins the components of the non whitespace back together.

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.

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.

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

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

How to cut a string at a specific point? [duplicate]

This question already has answers here:
NSString tokenize in Objective-C
(9 answers)
Closed 10 years ago.
So, I have a vast quantity of NSStrings and my problem is I need to cut them into smaller strings at a specific point. This may sound complicated but what I need basically is this:
NSString *test =" blah blah blah - goo goo goo.";
NSString *str1 = "blah blah blah ";
NSString *str2 = "goo goo goo";
How do I code for when there's a hyphen for the string to just cut off there. Is there a way to do this? I found ways to cut of the string after a certain amount of letters but I need it at the hyphen every time.
NSArray *arr = [string componentsSeparatedByString:#"-"];
should do the trick.
You could do this many ways. Two answers above show a few approaches. Many Objective-C solutions will include NSRange usage. You could also do more flexible things with NSScanner or NSRegularExpression.
There is not going to be one right answer.
NSString *cutString = [text substringFromIndex:3];
cutString = [text substringToIndex:5];
cutString = [text substringWithRange:NSMakeRange(3, 5)];