In my app, users have the ability to search for other users by entering an # and then some letters, at which point a popup will show with the potential usernames. I am stuck at the point where the user will select a username from the popup, which should replace the partially entered text with the selected username. For example, if I type:
hi #jo
and then select john from the table, then it should look like below
hi #john
But this is not happening. I have find one method called
stringByPaddingToLength, but it work for only one character of the replaced text. If I search with two characters, it gives the wrong output. My code is following:
-(void)storeData:(NSString *)strName
{
NSString *strText = textView.text;//the String which is enter in textview
NSRange startRange = [strText rangeOfString:#"#" options:NSBackwardsSearch];//find the range of '#' from currently pointed at backward
strText = [strText stringByPaddingToLength:(strName.length + strText.length - 1) withString:strName startingAtIndex:(startRange.location+startRange.length)];//the string after append the string
}
If I enter #ch and then select "Chandru" it gives the following output:
2015-05-05 19:55:02.789 [3733:131086] StrName::#chhandru
If I just enter #c and then select "Chandru" it gives the following (correct) output.
2015-05-05 19:55:58.371 [3733:131086] StrName::#chandru
How can I make it display the correct username, no matter how many characters were already entered?
You can use replaceOccurrencesOfString:withString:options:range: method on NSMutableString for this.
[strText replaceOccurrencesOfString:#"#jo" withString:#"#john" options: CaseInsensitiveSearch range:NSMakeRange(0, strText.length)] ;
Related
I have a small doubt on splitting a string into substring.
I want to display State and Country name in one label. i have a string in a service coming from json file "FullAddress": "New Windsor,New York,USA". i want only New York,USA to display in a label.
Use this and get your desire String from Array element 1 and 2
NSArray *strings = [FullAddress componentsSeparatedByString:#","];
Please Use this code it will be help you out
NSString *fullAddress=#"New Windsor,New York,USA";
NSRange range=[fullAddress rangeOfString:#","];
if (range.location!=NSNotFound) {
NSString *string=[fullAddress substringFromIndex:range.location+range.length];//this is address you want
NSLog(#"%#",string);
}
I want to find the index of the 8th occurrence of " " so that I could split my string there. However all I can find is this line of code that gives me an array of all the occurrences of " ". Is there a function I can call that would give me this information?
int numberOfOccurences = [[myListString componentsSeparatedByString:#" "] count];
Edit 1: So far this is the solution I came up with:
if(numberOfOccurences > 8)
{
//find index of place where you want to split by picking an
//arbitrary number and finding the first white space
int index = (int)[[myListString substringFromIndex:45] rangeOfString:#" "].location;
NSLog(#"Index: %i", index);
//make substring
NSString *substringList1 = [myListString substringToIndex:(45+index)];
NSString *substringList2 = [myListString substringFromIndex:(45+index)];
}
There is no method to find the 8th space, but there are the building blocks you need.
The NSString method rangeOfString:options:range: will find the first occurrence of its first argument within the range specified by its third argument, it returns a range for the match. You simply start with the third argument being the whole string and then iterate reducing the range to search using the previous result.
If you are actually looking for white space and not simply a space you might consider the similar rangeOfCharactersFromSet methods.
If you don't really want the eighth space, but are trying to break a string at a given length, you can look at componentsSeparatedByString/componentsSeparatedByCharactersInSet and then reassemble the resultant “words" into strings of the appropriate length. You might also want to look at NSScanner.
HTH
With the explicit assumption that you are looking for the eighth space (and possibly needing to adjust the regex a little depending on character set), you could use a regular expression:
NSRegularExpression *exp = [NSRegularExpression regularExpressionWithPattern:#"^([^ ]* ){8}+"
options:0
error:NULL];
NSRange result = [exp rangeOfFirstMatchInString:input options:0 range:inputRange];
If result.location != NSNotFound then result.length gives you the index on which to split.
I came across a question that had an example using rangeOfString. How ever the first thing that came to mind was NSPredicate.
I have different strings that return words separated by sentences. For example I have one that returns "Male, Female".
What is the most efficient way to search either "Male" or "Female". I'd like to perform some actions if the word happens to be part of the sentence and if it doesn't.
NSDictionary with stored words separated by commas. I use different keys to grab specific bunch of words. Below I use the "selectedGenders" key which returns "Male, Female":
if ([combinedRefinementSelection valueForKey:#"selectedGenders"]) {
NSString *selectedGenders = [combinedRefinementSelection valueForKey:#"selectedGenders"];
// Show string in label so customer knows how their clothes items will be filtered
[[_thisController chosenGender] setText:selectedGenders];
}
I simply want to search selectedGenders and find out if Male or Female is part of the string.
As you said, rangeOfString works just fine.
NSString* sentence = #"Male, Female";
if ([sentence rangeOfString:#"Male"].location != NSNotFound)
{
NSLog(#"Male is found");
}
if ([sentence rangeOfString:#"Female"].location != NSNotFound)
{
NSLog(#"Female is found");
}
So basically I want to use a UISearchBar to search an Array of Names.
Inside the Array the Names have this Format:
[FIRST NAME] ([SECOND NAME]) [LAST NAME]
Using
if (![string rangeOfString:seachTextPart].location == NSNotFound)
I have managed to search Names entering ONLY the FIRST NAME into the UISearchBar.
But as soon as I enter a SPACE i get NO RESULTS.
Also I want to be able to search by entering just the LAST NAME.
Can anyone please help me?
Thanks
Assuming your format is follow:name = #"[John]([James])[Smith]";
Searching [name rangeOfString:#"John"] would yield found.
Searching [name rangeOfString:#"John "] would yield not found, since #"John " is not in name
But in this case, [name rangeOfString:#"Smith"] would yield found.
Try this: Remove all spaces and lower case strings
searchTerm = [[searchTerm stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
NSRange searchName = [[name lowercaseString] rangeOfString:searchTerm];
If you want to test, just simply do this"
if (searchName == 0) //it does not match
Byte's code works unless they type "John Smith." If you want something more robust you can use - (NSArray *)componentsSeparatedByString:(NSString *)separator and then search for any of the strings in the Array. So Typing "John Smith" would return all of the Johns and all of the Smiths
here:
NSArray *theTerms = [searchTerm componentsSeparatedByString:#" "];
then run a for loop through all of theTerms and check them against your names adding the ones that contain the search terms to an array and then displaying them.
Is it possible to make a function that searchs a string for an exact substring so that it will only 'return true' if the exact string if found, not as part of a larger word?
NSString* search = #"tele";
NSString* stringOne = #"telephone";
NSString* stringTwo = #"tele phone";
NSString* stringThree = #"phone tele";
What I mean is: Is it possible to search for a string in a way that the NSString 'search' would be found in strings Two and Three, but not One?
Try using the following function in the NSString class:
- (NSRange)rangeOfString:(NSString *)aString
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html
Simplest approach is to append blanks (or whatever your separators are) to front and rear of both strings, then do the search.