How to fetch the last word from textfield [duplicate] - ios

This question already has answers here:
Getting the last word of an NSString
(7 answers)
Closed 8 years ago.
I have the following text in the text field
The Taj, Restraint, Renovation, Catch
How to get the Catch word alone from the text field.

NSString *str = #"The Taj, Restraint, Renovation, Catch";
NSString *lastWord = [[str componentsSeparatedByString:#","] lastObject];

Try this
NSString *myString = #"Taj, Restraint, Renovation, Catch" ;
__block NSString *lastWord = nil;
[myString enumerateSubstringsInRange:NSMakeRange(0, myString.length) options:(NSStringEnumerationByWords | NSStringEnumerationReverse) usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
lastWord = substring;
*stop = YES;
}];

Related

How to remove dots only from starting of string [duplicate]

This question already has answers here:
Swift: Remove specific characters of a string only at the beginning
(6 answers)
Closed 5 years ago.
My string text is Like -
1) .....bla ..bla...
2)...bla.. bla…bla.
3).bla.. bla…bla.
dots are not static.I want to remove dots only from starting. not All dots
I have tried with this
NSString *newString = [myString stringByReplacingOccurrencesOfString:#"." withString:#""];
but this is removing all dots.
try this.
NSString* regex = #"^\\.*";
NSString* input = #"....abc..z";
NSString* output = [input stringByReplacingOccurrencesOfString:regex withString:#"" options:NSRegularExpressionSearch range: [input rangeOfString:input]];
var test = "...abc";
while test.hasPrefix(".") {
test.remove(at: test.startIndex)
}
//test variable will have dots removed at the start.
Try this.
Try this:
NSString *str = #"...Videt...IDL";
__block NSInteger loc = 0;
[str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
loc = substringRange.location;
*stop = YES;
}];
NSString *convStr = [str substringFromIndex:loc];
NSLog(#"%#", convStr);
NSString *str = #"....bla .... bla..... bla";
NSCharacterSet *charSet = [NSCharacterSet letterCharacterSet];
int letterNO = 0;
for (int i =0; i<str.length; i++)
{
NSString *letter = [NSString stringWithFormat:#"%c",[str characterAtIndex:i]];
if ([letter rangeOfCharacterFromSet:charSet].location != NSNotFound)
{
letterNO = i;
break;
}
}
NSString * newString = [str substringFromIndex:letterNO];
NSLog(#"newString %#",newString);

How can I substring in a required format in ios [duplicate]

This question already has answers here:
How do you detect words that start with “#” or “#” within an NSString?
(6 answers)
Closed 7 years ago.
I have a string like this ,
NSString *strTest = #"Hii how are you doing #Ravi , how do u do #Kiran where are you #Varun";
I want a substring from the above string which contains only the words which starts with '#'
i.e I need
NSString *strSubstring = #"Ravi #kiran #varun";
Please help me out how could I achieve this
Separate the string like below :-
NSArray * names = [myString componentsSeparatedByString:#" "];
names array will now contain all the words in the string, now you can iterate over the array and check which of its index contains "#" character.
If you find "#", store that index value in some variable.
you will love implementing this code. I have used a regular expression to perform the job. It will give you the matching strings.
NSString *strTest = #"Hii how are you doing #Ravi , how do u do #Kiran where are you #Varun";
NSError *error;
//&[^;]*;
NSRegularExpression *exp =[NSRegularExpression regularExpressionWithPattern:#"#[^ ]*" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *s1= [exp matchesInString:strTest options:NSMatchingReportCompletion range:NSMakeRange(0, [strTest length]) ];
[s1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSTextCheckingResult *result = obj;
NSString *str = [strTest substringWithRange:result.range];
NSLog(#"str %#",str);
}];

Splitting NSString into two parts (before and after last slash) [duplicate]

This question already has answers here:
Split an NSString to access one particular piece
(7 answers)
Closed 7 years ago.
Is there a better (more concise/efficient/elegant) way than this for splitting a string that contains a slash into two parts: one before, the other after the last slash.
NSRange range = [s rangeOfString: #"/" options: NSBackwardsSearch];
NSAssert(range.location != NSNotFound, nil);
NSString *s1 = [s substringToIndex: range.location];
NSString *s2 = [s substringFromIndex: range.location + 1];
you can use
- componentsSeparatedByString:
eg
NSArray *components = [s componentsSeparatedByString:#"/"];
NSString *s1 = ((![components count])? nil :[components objectAtIndex:0] );
NSString *s2 = ((![components count]>0)? nil :[components objectAtIndex:1] );

Split an NSString in Objective-C with no delimiter [duplicate]

This question already has answers here:
Extract characters from NSString object
(3 answers)
Closed 9 years ago.
I know there are several answers on splitting strings using componentsSeparatedByString:, but what if the string has no delimiter. using #"", doesn't work.
NSString *str = #"ABCDE";
NSArray *arr = [str componentsSeparatedByString:#""];
NSLog(#"%#", arr)
==> ABCDE
What I want is -
==> (A,B,C,D,E)
So that I can access each character. Thanks!
The most reliable technique is to use -[NSString enumerateSubstringsInRange:options:usingBlock:] with the NSStringEnumerationByComposedCharacterSequences option.
NSMutableArray* array = [NSMutableArray array];
[str enumerateSubstringsInRange:NSMakeRange(0, str.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
[array addObject:substring];
}];
Either use [string characterAtIndex:i]; in a for loop or
- (NSString *)substringWithRange:(NSRange)aRange in a loop.
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i = 0; i < string.length; i++)
{
unsigned int character = [string characterAtIndex:i];
[arr addObject:[NSString stringWithFormat:#"%C",character]];
}

How to get NSRange(s) for a substring in NSString? [duplicate]

This question already has answers here:
How to get all NSRange of a particular character in a NSString?
(4 answers)
Closed 9 years ago.
NSString *str = #" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot...";
How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string.
NSRange range = NSMakeRange(0, _stringLength);
while(range.location != NSNotFound)
{
range = [[attString string] rangeOfString: #"California" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, _stringLength - (range.location + range.length));
[attString addAttribute:NSForegroundColorAttributeName value:_green range:range];
}
}
Lots of ways of solving this problem - NSScanner was mentioned; rangeOfString:options:range etc. For completeness' sake, I'll mention NSRegularExpression. This also works:
NSMutableAttributedString *mutableString = nil;
NSString *sampleText = #"I live in California, blah blah blah California.";
mutableString = [[NSMutableAttributedString alloc] initWithString:sampleText];
NSString *pattern = #"(California)";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
// enumerate matches
NSRange range = NSMakeRange(0,[sampleText length]);
[expression enumerateMatchesInString:sampleText options:0 range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange californiaRange = [result rangeAtIndex:0];
[mutableString addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:californiaRange];
}];
with
[str rangeOfString:#"California"]
and
[str rangeOfString:#"California" options:YOUR_OPTIONS range:rangeToSearch]
You may use rangeOfString:options:range: or NSScanner (there are other possibilities like regexps but anyway). It's easier to use first approach updating range, i.e. search for first occurrence and then depending on the result update the search range. When the search range is empty, you've found everything;

Resources