How to find some words in a HTML page - iphone

I have a problem with this code:
textRange = [[html lowercaseString] rangeOfString:[substring lowercaseString]];
I'd like to find some words (substrings) in a HTML page. Using this method, I can find the words that are defined in "substring" but I want to find only entire words which are the substring to be searched for, and not any arbitrary (unbounded) occurrences of the substring. So I tried this method but nothing has changed:
textRange = [html rangeOfString:substring options:NSLiteralSearch];
Here's the entire code:
NSRange textRange;
textRange = [[html lowercaseString] rangeOfString:[substring lowercaseString]];
if (textRange.location != NSNotFound) {
check++;
} else {
// webView.hidden = NO;
}

Use NSRegularExpression and include \b at the start and end of the pattern to match word boundaries.

Related

How to validate the textfield with 8 to 15 characters and with strong password [duplicate]

This question already has answers here:
Regular Expression for password in iPhone
(2 answers)
Closed 9 years ago.
I Have a text field that I want to enter password in that.I want to enter strong password.That means 8 to 15 characters in that at least one small Letter,one Capital Letter,1 spacial caracter,one number.
Please give the suggestion.
With
password.length
you can ask for the length of a string. Compare that to your desired limits.
With
- (BOOL)string:(NSString *)text matches:(NSString *)pattern
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
return matches.count > 0;
}
you have a method that provides regex to strings (you can also implement this as a category to NSString).
The first parameter will be your password, the second will be the pattern.
I am not that good with regex, so there might be better solutions but this would be my way
NSString *password = #"iS_bhd97zAA!";
NSString *scPattern = #"[a-z]";
NSString *cPattern = #"[A-Z]";
NSString *sPattern = #"[!%&\._;,]";
NSString *nPattern = #"[0-9]";
if (8 <= password.length && password.length <= 15 &&
[self string:password matches:scPattern] &&
[self string:password matches:cPattern] &&
[self string:password matches:sPattern] &&
[self string:password matches:nPattern])
{
NSLog(#"PW is valid");
}
Hint
The regex for special characters is tricky because you need to escape some of the chars. Mine might be correct, but I am not absolutely sure.
There is also a possiblility to do this in only one regex, but this looks scary imo
This one
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
has everything except the special chars, maybe you want to add that yourself :D
This can be easily done using Regular Expressions. I'm not familiar with Regular Expressions, so I'm suggesting this hard way.
You can use this function for checking this:
- (BOOL)strongPassword:(NSString *)yourText
{
BOOL strongPwd = YES;
//Checking length
if([yourText length] < 8)
strongPwd = NO;
//Checking uppercase characters
NSCharacterSet *charSet = [NSCharacterSet uppercaseLetterCharacterSet];
NSRange range = [yourText rangeOfCharacterFromSet:charSet];
if(range.location == NSNotFound)
strongPwd = NO;
//Checking lowercase characters
charSet = [NSCharacterSet lowercaseLetterCharacterSet];
range = [yourText rangeOfCharacterFromSet:charSet];
if(range.location == NSNotFound)
strongPwd = NO;
//Checking special characters
charSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
range = [yourText rangeOfCharacterFromSet:charSet];
if(range.location == NSNotFound)
strongPwd = NO;
return strongPwd;
}
In the delegate method textField:shouldChangeCharactersInRange:replacementString: for the UITextField you need to do the following:
Get text with the property myTextField.text
Check for uppercase and lower case character counts
Similarly check for the special character and the number counts
If all conditions are satisfied process the field else display error.
I would have written down the code for you but its against the SO policies. You attempt writing this code and if you are stuck you can always post another question.

Replace string by an image

I am trying to use emoticons for my chat application. I am comparing the string I get or I send with a local string. If I am get the result as true I want to replace the string with an image. For that matter I am not using may emoticons.
I am trying this:
NSRange textRange;
textRange =[text rangeOfString:#":)"];
if(textRange.location != NSNotFound)
{
//Does contain the substring
}
else
{
// replace string with image.
}
But I am not able to figure out how to replace image at the string position.
I assumed you want to put smileys.
So you can try this :
NSRange range = {NSNotFound, 0};
NSString *s = #"This is a smiley :) face";
range.location = 0;
range.length = [s length];
s = [s stringByReplacingOccurrencesOfString:#":)"
withString:#"\ue415"
options:NSCaseInsensitiveSearch
range:range];
For more detail please refer this link.

NSString search whole text for another string

I would like to search for an NSString in another NSString, such that the result is found even if the second one does not start with the first one, for example:
eg: I have a search string "st". I look in the following records to see if any of the below contains this search string, all of them should return a good result, because all of them have "st".
Restaurant
stable
Kirsten
At the moment I am doing the following:
NSComparisonResult result = [selectedString compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
This works only for "stable" in the above example, because it starts with "st" and fails for the other 2. How can I modify this search so that it returns ok for all the 3?
Thanks!!!
Why not google first?
String contains string in objective-c
NSString *string = #"hello bla bla";
if ([string rangeOfString:#"bla"].location == NSNotFound) {
NSLog(#"string does not contain bla");
} else {
NSLog(#"string contains bla!");
}
Compare is used for testing less than/equal/greater than. You should instead use -rangeOfString: or one of its sibling methods like -rangeOfString:options:range:locale:.
I know this is an old thread thought it might help someone.
The - rangeOfString:options:range: method will allow for case insensitive searches on a string and replace letters like ‘ö’ to ‘o’ in your search.
NSString *string = #"Hello Bla Bla";
NSString *searchText = #"bla";
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSRange searchRange = NSMakeRange(0, string.length);
NSRange foundRange = [string rangeOfString:searchText options:searchOptions range:searchRange];
if (foundRange.length > 0) {
NSLog(#"Text Found.");
}
For more comparison options NSString Class Reference
Documentation on the method - rangeOfString:options:range: can be found on the NSString Class Reference

How to check if a string contains an URL

i have text message and I want to check whether it is containing text "http" or URL exists in that.
How will I check it?
NSString *string = #"xxx http://someaddress.com";
NSString *substring = #"http:";
Case sensitive example:
NSRange textRange = [string rangeOfString:substring];
if(textRange.location != NSNotFound){
//Does contain the substring
}else{
//Does not contain the substring
}
Case insensitive example:
NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];
if(textRange.location != NSNotFound){
//Does contain the substring
}else{
//Does not contain the substring
}
#Cyprian offers a good option.
You could also consider using a NSRegularExpression which would give you far more flexibility assuming that's what you need, e.g. if you wanted to match http:// and https://.
Url usually has http or https in it
You can use your custom method containsString to check for those strings.
- (BOOL)containsString:(NSString *)string {
return [self containsString:string caseSensitive:NO];
}
- (BOOL)containsString:(NSString*)string caseSensitive:(BOOL)caseSensitive {
BOOL contains = NO;
if (![NSString isNilOrEmpty:self] && ![NSString isNilOrEmpty:string]) {
NSRange range;
if (!caseSensitive) {
range = [self rangeOfString:string options:NSCaseInsensitiveSearch];
} else {
range = [self rangeOfString:string];
}
contains = (range.location != NSNotFound);
}
return contains;
}
Example :
[yourString containsString:#"http"]
[yourString containsString:#"https"]

How to test a string for text

I would like to test a string to see if anywhere it contains the text "hello". I would like the test to not take into account capitalization. How can I test this string?
Use the below code as reference to find check for a substring into a string.
NSString* string = #"How to test a string for text" ;
NSString* substring = #"string for" ;
NSRange textRange;
textRange =[string rangeOfString:substring options:NSCaseInsensitiveSearch];
if(textRange.location != NSNotFound)
{
//Does contain the substring
}
-[NSString rangeOfString: options:] will do it.
NSRange range = [string rangeOfString:#"hello" options:NSCaseInsensitiveSearch];
BOOL notFound = range.location==NSNotFound;
I am assuming all words are separated by a space, and that there is no punctuation. If there is punctuation.
NSArray *dataArray = [inputString componentsSeparatedByString:#" "];
for(int i=0; i<[dataArray count]){
if([[dataArray objectAtIndex:i] isEqualToString:#"hello"]){
NSLog(#"hello has been found!!!");
}
}
I haven't tested this but it should work in theory.
Check out the docs for ways to remove punctuation and make the string all lower case. This should be pretty straight-forward.
Other solutions here are good but you should really use a regex,
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"^(hello)*$"
options:NSRegularExpressionCaseInsensitive
error:&error];
Docs are here: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html