How to do a reverse search to find the 2nd space / space and replace it with another line?

it's me again. I asked a question similar to this some time ago, but this question is a bit more complicated. I planned to use RegexKitLite to do what I needed, but I believe that this can be done without it. I have an NSString in which there are a few words with spaces / spaces, and I want to get the last space in the line that is to the left of the last word. Example Line below:

NSString *string = @"Here is an example string HELLO "; 

As you can see in the line above, there is a space / space at the very end of the line. I want to get a space / space to the left of HELLO and replace it with my own text / line. I am working on using NSString NSBackwardsSearch, but it does not work.

 NSString *spaceReplacement = @"text that i want"; NSString *replaced = [snipet [string rangeOfString:substring options:NSBackwardsSearch].location:@" " withString:spaceReplacement]; NSLog(@"%@", replaced); 

Any help would help, I'm just tired of trying to fix it, it made me intimidated. I thought I could do it with RegexKitLite, but the learning curve for this is too steep for me, given my time frame with which I work. I am glad that Jacob R. suggested that I use the NSString methods :-)

+4
source share
3 answers

This solution assumes that you always have a space at the end of the line ... it should convert

Here is an example HELLO line

... to:

Here is an example stringtext I want HELLO

... since what I understood, you wanted to do.

Here is the code:

 NSString *string = @"Here is an example string HELLO "; NSRange rangeToSearch = NSMakeRange(0, [string length] - 1); // get a range without the space character NSRange rangeOfSecondToLastSpace = [string rangeOfString:@" " options:NSBackwardsSearch range:rangeToSearch]; NSString *spaceReplacement = @"text that i want"; NSString *result = [string stringByReplacingCharactersInRange:rangeOfSecondToLastSpace withString:spaceReplacement]; 

The trick is to use the [NSString rangeOfString:options:range:] method.

Note. If the line does not always contain a space at the end, this code will probably fail, and you will need code that is a bit more complicated. If so, let me know and I will update the answer.

Disclaimer: I have not tested the code, but it should compile and work very well.

+17
source

Something like this should work:

 NSString *string = @"Here is an example string HELLO "; if ([string hasSuffix:@" "]) { NSString *spaceReplacement = @"text that i want"; NSString *replacedString = [[string substringToIndex: [string length]] stringByAppendingString:spaceReplacement]; NSLog(@"replacedString == %@", replacedString); } 
+1
source

To solve @Senseful note

If the line does not always contain a space at the end, this code will probably fail, and you will need code that is a bit more complicated. If so, let me know and I will update the answer.

I added one line to the code, which helps in such situations and makes the code more universal:

 // Some income sting NSString * string = @"Here is an example string HELLO "; // clear string from whitespace in end on string [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSRange rangeToSearch = NSMakeRange(0, [string length]); NSRange rangeOfSecondToLastSpace = [string rangeOfString:@" " options:NSBackwardsSearch range:rangeToSearch]; // Replaed with String NSString * spaceReplacement = @"text that i want"; NSString * result = [string stringByReplacingCharactersInRange:rangeOfSecondToLastSpace withString:spaceReplacement]; 
0
source

Source: https://habr.com/ru/post/1333117/


All Articles