I want to check the string if it contains alphanumeric only.(Both letters and number only).
I tried using NSCharacterSet *strCharSet = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"];
I also tried this NSString *string = #"[^a-zA-Z0-9]"; = no luck
but it only checks if the string does not contain any characters or it contains only this characters. I need to required the user to put both letters and numbers.
-update
I have a textfield for password. I need the user to input both letters and password. But I can't check if the textfield contains BOTH letters and numbers. By using the code above, it only checks if the textfield's text contains only alphanumeric.
You just need to make your checks for numbers and letters separate.
BOOL containsLetter = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.letterCharacterSet].location;
BOOL containsNumber = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet].location;
NSLog(#"Contains letter: %d\n Contains number: %d", containsLetter, containsNumber);
Related
I am using Zxing library to scan the Barcodes. The result is stored in an NSString. Here I am looking into two cases:
Case:'semicolon' : If the result string contains semicolon character....then store it in Semicolon Array
myWords_semicolon = [_myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#";,;;"]
];
//here myWords_semicolon is a NSArray
Case: 'pipe' : If the result string contains pipe character...then store it in a pipe array.
myWords_pipe = [_myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"|,||"]
];
What I tried to do is, if the result string contains semicolon......go to case :'semicolon' ......if the result contains pipe go to case: 'pipe'. I used this to do that but couldn't get the right solution.
if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#";,;;"]].location != NSNotFound) {
NSLog(#"This string doesnt contain semicolon characters");
myWords=myWords_pipe;
}
if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"|,||"]].location != NSNotFound) {
NSLog(#"This string doesnt contain pipe characters");
myWords=myWords_semicolon;
}
Here in this case....only semicolon is case is working,even though I scan pipe case ,the scanner is unable to recognize the pipe case.. Is there any other way to use && or || logic here?
The problem with your code is that both sets contain commas, and at the same time both strings with pipes and strings with semicolons contain commas. Therefore, only the assignment from the last of the two ifs is going to have an effect, because both ifs will "fire".
You should be able to fix this by removing commas and duplicate pipes from your sets. Moreover, you should be able to simplify it further by using rangeOfString: method instead of rangeOfCharacterFromSet:
if ([_myString rangeOfString:#";"].location != NSNotFound) {
NSLog(#"This string contains a semicolon");
myWords=myWords_semicolon;
}
if ([_myString rangeOfString:#"|"].location != NSNotFound) {
NSLog(#"This string contains a pipe");
myWords=myWords_pipe;
}
I simply need to remove the last digit from a string that contains numbers.
Say that my mutable string is named scannedItem and produces the following 038000768625.
How can I remove the last digit, and put it into a new string that produces 03800076862?
Thanks
Create a new string from old one. Firstly by removing last character
NSString *newString = [scannedItem substringToIndex:[scannedItem length]-1];
Now append
//Append string of your choice
newString = [newString stringByAppendingString:#"?"];
I am taking input from textfield ,i has to extract the characters more than length 5. Then how to remove characters from text
You can try this,
if(level_label.text.length>20)
{
NSString *str=[level_label.text substringFromIndex:20];
// Place str in next_label.text
}
I'm trying to check the suffix of a string against a character set but I'm getting errors:
if ([string hasSuffix:[NSCharacterSet characterSetWithCharactersInString:#"(+-*/"])
What am I doing wrong, is there a correct alternative?
-[NSString hasSuffix:] takes an NSString as its argument, not an NSCharacterSet. Your best bet is probably to call -[NSString rangeOfCharacterFromSet:options:] with NSBackwardsSearch as an option and check that the location is at the end of the string.
What are you trying to do?
I think that you are trying to see if the first letter of your string is one of the characters from the set. If so, then use this:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"(+-*/"];
if ([[string substringToIndex:1] rangeOfCharacterFromSet:set].location != NSNotFound)
// String starts with one of the characters from the character set
In Objective-C for iOS, how would I remove the last character of a string using a button action?
In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:
if ([string length] > 0) {
string = [string substringToIndex:[string length] - 1];
} else {
//no characters to delete... attempting to do so will result in a crash
}
If you want a fancy way of doing this in just one line of code you could write it as:
string = [string substringToIndex:string.length-(string.length>0)];
*Explanation of fancy one-line code snippet:
If there is a character to delete (i.e. the length of the string is greater than 0)
(string.length>0) returns 1 thus making the code return:
string = [string substringToIndex:string.length-1];
If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
(string.length>0) returns 0 thus making the code return:
string = [string substringToIndex:string.length-0];
Which prevents crashes.
If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:
[myString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];
The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.
In fact, the iOS header file which contains the declaration of substringToIndex contains the following comment:
Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters
See how to use rangeOfComposedCharacterSequenceAtIndex: to delete the last character correctly.
The documentation is your friend, NSString supports a call substringWithRange that can shorten the string that you have an return the shortened String. You cannot modify an instance of NSString it is immutable. If you have an NSMutableString is has a method called deleteCharactersInRange that can modify the string in place
...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...