xcode 如何进行向后搜索以找到第二个空格/空白并将其替换为另一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4515058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a backwards search to find the 2nd space/blank and replace it with another string?
提问by 0SX
it's me again. I've asked a question similar to this just awhile ago but this question is a bit more complex. I was planning on using RegexKitLite to do what I needed to do but I believe this can be done with out it. I have a NSString that has some words with spaces/blanks in it and I'm wanting to get the very last space in the string that is to the left of the last word. Example String below:
又是我。我刚才问过一个类似的问题,但这个问题有点复杂。我打算使用 RegexKitLite 来做我需要做的事情,但我相信不用它也可以做到。我有一个 NSString ,其中有一些带有空格/空格的单词,我想获得最后一个单词左侧的字符串中的最后一个空格。下面的示例字符串:
NSString *string = @"Here is an example string HELLO ";
As you can see in the string above there is a space/blank at the very end of the string. I'm wanting to be able to get the space/blank to the left of HELLO and replace it with my own text/string. I'm working on using the NSString's NSBackwardsSearch but it's not working.
正如您在上面的字符串中看到的那样,字符串的最末端有一个空格/空白。我希望能够获得 HELLO 左侧的空格/空白并将其替换为我自己的文本/字符串。我正在使用 NSString 的 NSBackwardsSearch 但它不起作用。
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 this thing, it's driving me bonkers. I thought I could do this with RegexKitLite but the learning curve for that is too steep for me considering my timeframe I'm working with. I'm glad Jacob R. referred me to use NSString's methods :-)
任何帮助都会有所帮助,我只是厌倦了试图解决这个问题,它让我发疯。我以为我可以用 RegexKitLite 做到这一点,但考虑到我正在使用的时间范围,学习曲线对我来说太陡峭了。我很高兴 Jacob R. 推荐我使用 NSString 的方法:-)
回答by Senseful
This solution assumes you always have a space at the end of your string... it should convert
这个解决方案假设你的字符串末尾总是有一个空格......它应该转换
Here is an example string HELLO
这是一个示例字符串 HELLO
... to:
... 到:
Here is an example stringtext that i wantHELLO
这是我想要的示例字符串文本HELLO
... since that's what I understood you wanted to do.
......因为那是我理解你想要做的。
Here's 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.
诀窍是使用[NSString rangeOfString:options:range:]
方法。
Note: If the string doesn't always contain a space at the end, this code will probably fail, and you would need code that is a bit more complicated. If that is the case, let me know and I'll update the answer.
注意:如果字符串的末尾并不总是包含空格,则此代码可能会失败,并且您需要更复杂的代码。如果是这种情况,请告诉我,我会更新答案。
Disclaimer: I haven't tested the code, but it should compile and work just fine.
免责声明:我还没有测试过代码,但它应该可以编译并正常工作。
回答by NSGod
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);
}
回答by Nazir
To solve @Senseful note
解决@Senseful笔记
If the string doesn't always contain a space at the end, this code will probably fail, and you would need code that is a bit more complicated. If that is the case, let me know and I'll update the answer.
如果字符串的末尾并不总是包含空格,则此代码可能会失败,并且您需要更复杂的代码。如果是这种情况,请告诉我,我会更新答案。
I had added one line into code that helps in such situations and make 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];