Counting occurrences of capital letters and numbers in an NSString - objective-c

In PHP I am using the following code...
$passwordCapitalLettersLength = strlen(preg_replace("![^A-Z]+!", "", $password));
$passwordNumbersLength = strlen(preg_replace("/[0-9]/", "", $password));
...to count how many times capital letters and numbers appear in the password.
What is the equivalent of this in Objective C?

You can use the NSCharacterSet:
NSString *password = #"aas2dASDasd1asdASDasdas32D";
int occurrenceCapital = 0;
int occurenceNumbers = 0;
for (int i = 0; i < [password length]; i++) {
if([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:i]])
occurenceCapital++;
if([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[password characterAtIndex:i]])
occurenceNumbers++;
}

This can be done fairly concisely using the abilities of NSString and NSCharacterSet, instead of the need to iterate manually.
A decrement of 1 is required as componentsSeparatedByCharactersInSet: will always return at least one element, and that one element won't count your separations.
NSString* password = #"dhdjGHSJD7d56dhHDHa7d5bw3/%£hDJ7hdjs464525";
NSArray* capitalArr = [password componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
NSLog(#"Number of capital letters: %ld", (unsigned long) capitalArr.count - 1);
NSArray* numericArr = [password componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
NSLog(#"Number of numeric digits: %ld", (unsigned long) numericArr.count - 1);
Original answer: Whilst the code you've provided won't cover all bases, if you need to keep using those regular expressions for safety/risk reasons, you can do so below.
You can use RegEx in Objective-C. Saves manually iterating through the String, and keeps the code concise. It also means because you aren't iterating manually, you could potentially get a performance boost, as you can let the compiler/framework writer optimize it.
// Testing string
NSString* password = #"dhdjGHSJD7d56dhHDHa7d5bw3/%£hDJ7hdjs464525";
NSRegularExpression* capitalRegex = [NSRegularExpression regularExpressionWithPattern:#"[A-Z]"
options:0
error:nil];
NSRegularExpression* numbersRegex = [NSRegularExpression regularExpressionWithPattern:#"[0-9]"
options:0
error:nil];
NSLog(#"Number of capital letters: %ld", (unsigned long)[capitalRegex matchesInString:password
options:0
range:NSMakeRange(0, password.length)].count);
NSLog(#"Number of numeric digits: %ld", (unsigned long)[numbersRegex matchesInString:password
options:0
range:NSMakeRange(0, password.length)].count);

Related

Take all numbers separated by spaces from a string and place in an array

I have a NSString formatted like this:
"Hello world 12 looking for some 56"
I want to find all instances of numbers separated by whitespace and place them in an NSArray. I dont want to remove the numbers though.
Whats the best way of achieving this?
This is a solution using regular expression as suggested in the comment.
NSString *string = #"Hello world 12 looking for some 56";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:#"\\b\\d+" options:nil error:nil];
NSArray *matches = [expression matchesInString:string options:nil range:(NSMakeRange(0, string.length))];
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
[result addObject:[string substringWithRange:match.range]];
}
NSLog(#"%#", result);
First make an array using NSString's componentsSeparatedByString method and take reference to this SO question. Then iterate the array and refer to this SO question to check if an array element is number: Checking if NSString is Integer.
I don't know where you are looking to do perform this action because it may not be fast (such as if it's being called in a table cell it may be choppy) based upon the string size.
Code:
+ (NSArray *)getNumbersFromString:(NSString *)str {
NSMutableArray *retVal = [NSMutableArray array];
NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
NSString *placeholder = #"";
unichar currentChar;
for (int i = [str length] - 1; i >= 0; i--) {
currentChar = [str characterAtIndex:i];
if ([numericSet characterIsMember:currentChar]) {
placeholder = [placeholder stringByAppendingString:
[NSString stringWithCharacters:&currentChar
length:[placeholder length]+1];
} else {
if ([placeholder length] > 0) [retVal addObject:[placeholder intValue]];
else placeholder = #"";
return [retVal copy];
}
To explain what is happening above, essentially I am,
going through every character until I find a number
adding that number including any numbers after to a string
once it finds a number it adds it to an array
Hope this helps please ask for clarification if needed

Check if NSString only contains one character repeated

I want to know a simple and fast way to determine if all characters in an NSString are the same.
For example:
NSString *string = "aaaaaaaaa"
=> return YES
NSString *string = "aaaaaaabb"
=> return NO
I know that I can achieve it by using a loop but my NSString is long so I prefer a shorter and simpler way.
you can use this, replace first character with null and check lenght:
-(BOOL)sameCharsInString:(NSString *)str{
if ([str length] == 0 ) return NO;
return [[str stringByReplacingOccurrencesOfString:[str substringToIndex:1] withString:#""] length] == 0 ? YES : NO;
}
Here are two possibilities that fail as quickly as possible and don't (explicitly) create copies of the original string, which should be advantageous since you said the string was large.
First, use NSScanner to repeatedly try to read the first character in the string. If the loop ends before the scanner has reached the end of the string, there are other characters present.
NSScanner * scanner = [NSScanner scannerWithString:s];
NSString * firstChar = [s substringWithRange:[s rangeOfComposedCharacterSequenceAtIndex:0]];
while( [scanner scanString:firstChar intoString:NULL] ) continue;
BOOL stringContainsOnlyOneCharacter = [scanner isAtEnd];
Regex is also a good tool for this problem, since "a character followed by any number of repetitions of that character" is in very simply expressed with a single back reference:
// Match one of any character at the start of the string,
// followed by any number of repetitions of that same character
// until the end of the string.
NSString * patt = #"^(.)\\1*$";
NSRegularExpression * regEx =
[NSRegularExpression regularExpressionWithPattern:patt
options:0
error:NULL];
NSArray * matches = [regEx matchesInString:s
options:0
range:(NSRange){0, [s length]}];
BOOL stringContainsOnlyOneCharacter = ([matches count] == 1);
Both these options correctly deal with multi-byte and composed characters; the regex version also does not require an explicit check for the empty string.
use this loop:
NSString *firstChar = [str substringWithRange:NSMakeRange(0, 1)];
for (int i = 1; i < [str length]; i++) {
NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
if(![ch isEqualToString:firstChar])
{
return NO;
}
}
return YES;

Replace specific words in NSString

what is the best way to get and replace specific words in string ?
for example I have
NSString * currentString = #"one {two}, thing {thing} good";
now I need find each {currentWord}
and apply function for it
[self replaceWord:currentWord]
then replace currentWord with result from function
-(NSString*)replaceWord:(NSString*)currentWord;
The following example shows how you can use NSRegularExpression and enumerateMatchesInString to accomplish the task. I have just used uppercaseString as function that replaces a word, but you can use your replaceWord method as well:
EDIT: The first version of my answer did not work correctly if the replaced words are
shorter or longer as the original words (thanks to Fabian Kreiser for noting that!) .
Now it should work correctly in all cases.
NSString *currentString = #"one {two}, thing {thing} good";
// Regular expression to find "word characters" enclosed by {...}:
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:#"\\{(\\w+)\\}"
options:0
error:NULL];
NSMutableString *modifiedString = [currentString mutableCopy];
__block int offset = 0;
[regex enumerateMatchesInString:currentString
options:0
range:NSMakeRange(0, [currentString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// range = location of the regex capture group "(\\w+)" in currentString:
NSRange range = [result rangeAtIndex:1];
// Adjust location for modifiedString:
range.location += offset;
// Get old word:
NSString *oldWord = [modifiedString substringWithRange:range];
// Compute new word:
// In your case, that would be
// NSString *newWord = [self replaceWord:oldWord];
NSString *newWord = [NSString stringWithFormat:#"--- %# ---", [oldWord uppercaseString] ];
// Replace new word in modifiedString:
[modifiedString replaceCharactersInRange:range withString:newWord];
// Update offset:
offset += [newWord length] - [oldWord length];
}
];
NSLog(#"%#", modifiedString);
Output:
one {--- TWO ---}, thing {--- THING ---} good

Count the amount of times '$' shows up in a string (Objective C)

I was wondering if there was an easy method to find the amount of times a character such as '$' shows up in a string in the language objective-c.
The real world example I am using is a string that would look like:
542$764$231$DataEntry
What I need to do is first:
1) count the amount of times the '$' shows up to know what tier the DataEntry is in my database (my database structure is one I made up)
2) then I need to get all of the numbers, as they are index numbers. The numbers need to be stored in a NSArray. And I will loop through them all getting the different indexes. I'm not going to explain how my database structure works as that isn't relevant.
Basically from that NSString, I need, the amount of times '$' shows up. And all of the numbers in between the dollar signs. This would be a breeze to do in PHP, but I was curious to see how I could go about this in Objective-C.
Thanks,
Michael
[[#"542$764$231$DataEntry" componentsSeparatedByString:#"$"] count]-1
The componentsSeparatedByString suggested by #Parag Bafna and #J Shapiro or NSRegularExpression e.g.:
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
#autoreleasepool {
NSError *error = NULL;
NSString *searchText = #"542$764$231$DataEntry";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"(\\d{3})\\$" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:searchText options:0 range:NSMakeRange(0, [searchText length]) ];
printf("match count = %ld\n",numberOfMatches);
[regex enumerateMatchesInString:searchText
options:0
range:NSMakeRange(0,[searchText length])
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSRange range = [match rangeAtIndex:1];
printf("match = %s\n",[[searchText substringWithRange:range] UTF8String]);
}];
}
}
The componentsSeparatedByString is probably the preferred approach and much more performant where the pattern has simple repeating delimiters; but I included this approach for completeness sake.
Try this code:
NSMutableArray* substrings=[NSMutableArray new];
// This will contain all the substrings
NSMutableArray* numbers=[NSMutableArray new];
// This will contain all the numbers
NSNumberFormatter* formatter=[NSNumberFormatter new];
// The formatter will scan all the strings and estabilish if they're
// valid numbers, if so it will produce a NSNumber object
[formatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString* entry= #"542$764$231$DataEntry";
NSUInteger count=0,last=0;
// count will contain the number of '$' characters found
NSRange range=NSMakeRange(0, entry.length);
// range is the range where to check
do
{
range= [entry rangeOfString: #"$" options: NSLiteralSearch range: range];
// Check for a substring
if(range.location!=NSNotFound)
{
// If there's not a further substring range.location will be NSNotFound
NSRange substringRange= NSMakeRange(last, range.location-last);
// Get the range of the substring
NSString* substring=[entry substringWithRange: substringRange];
[substrings addObject: substring];
// Get the substring and add it to the substrings mutable array
last=range.location+1;
range= NSMakeRange(range.location+range.length, entry.length-range.length-range.location);
// Calculate the new range where to check for the next substring
count++;
// Increase the count
}
}while( range.location!=NSNotFound);
// Now count contains the number of '$' characters found, and substrings
// contains all the substrings separated by '$'
for(NSString* substring in substrings)
{
// Check all the substrings found
NSNumber* number;
if([formatter getObjectValue: &number forString: substring range: nil error: nil])
{
// If the substring is a valid number, the method returns YES and we go
// inside this scope, so we can add the number to the numbers array
[numbers addObject: number];
}
}
// Now numbers contains all the numbers found

Replace characters in NSString

I am trying to replace all characters except last 4 in a String with *'s.
In objective-c there is a method in NSString class replaceStringWithCharactersInRange: withString: where I would give it range (0,[string length]-4) ) with string #"*". This is what it does: 123456789ABCD is modified to *ABCD while I am looking to make ********ABCD.
I understand that it replaced range I specified with string object. How to accomplish this ?
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\\d" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#"*"];
This looks like a simple problem... get the first part string and return it with the last four characters appended to it.
Here is a function that returns the needed string :
-(NSString *)neededStringWithString:(NSString *)aString {
// if the string has less than or 4 characters, return nil
if([aString length] <= 4) {
return nil;
}
NSUInteger countOfCharToReplace = [aString length] - 4;
NSString *firstPart = #"*";
while(--countOfCharToReplace) {
firstPart = [firstPart stringByAppendingString:#"*"];
}
// range for the last four
NSRange lastFourRange = NSMakeRange([aString length] - 4, 4);
// return the combined string
return [firstPart stringByAppendingString:
[aString substringWithRange:lastFourRange]];
}
The most unintuitive part in Cocoa is creating the repeating stars without some kind of awkward looping. stringByPaddingToLength:withString:startingAtIndex: allows you to create a repeating string of any length you like, so once you have that, here's a simple solution:
NSInteger starUpTo = [string length] - 4;
if (starUpTo > 0) {
NSString *stars = [#"" stringByPaddingToLength:starUpTo withString:#"*" startingAtIndex:0];
return [string stringByReplacingCharactersInRange:NSMakeRange(0, starUpTo) withString:stars];
} else {
return string;
}
I'm not sure why the accepted answer was accepted, since it only works if everything but last 4 is a digit. Here's a simple way:
NSMutableString * str1 = [[NSMutableString alloc]initWithString:#"1234567890ABCD"];
NSRange r = NSMakeRange(0, [str1 length] - 4);
[str1 replaceCharactersInRange:r withString:[[NSString string] stringByPaddingToLength:r.length withString:#"*" startingAtIndex:0]];
NSLog(#"%#",str1);
You could use [theString substringToIndex:[theString length]-4] to get the first part of the string and then combine [theString length]-4 *'s with the second part. Perhaps their is an easier way to do this..
NSMutableString * str1 = [[NSMutableString alloc]initWithString:#"1234567890ABCD"];
[str1 replaceCharactersInRange:NSMakeRange(0, [str1 length] - 4) withString:#"*"];
NSLog(#"%#",str1);
it works
The regexp didn't work on iOS7, but perhaps this helps:
- (NSString *)encryptString:(NSString *)pass {
NSMutableString *secret = [NSMutableString new];
for (int i=0; i<[pass length]; i++) {
[secret appendString:#"*"];
}
return secret;
}
In your case you should stop replacing the last 4 characters. Bit crude, but gets the job done