Objective C: How to extract part of a String (e.g.

2019-01-08 11:14发布

I have a string as shown below,

NSString * aString = @"This is the #substring1 and #subString2 I want";

How can I select only the text starting with '#' (and ends with a space), in this case 'subString1' and 'subString2'?

Note: Question was edited for clarity

7条回答
男人必须洒脱
2楼-- · 2019-01-08 11:53

Pretty simple, easy to understand version avoiding NSRange stuff:

NSArray * words = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
NSMutableArray * mutableWords = [NSMutableArray new];
for (NSString * word in words){
    if ([word length] > 1 && [word characterAtIndex:0] == '#'){
        NSString * editedWord = [word substringFromIndex:1];
        [mutableWords addObject:editedWord];
    }
}
查看更多
叛逆
3楼-- · 2019-01-08 11:56

Assuming that you are looking to find the first string that starts with a pound, and ends with a space, this might work. I don't have XCode in front of me, so forgive me if there's a syntax error or length off by 1 somewhere:

-(NSString *)StartsWithPound:(NSString *)str {
    NSRange range = [str rangeOfString:@"#"];
    if(range.length) {
        NSRange rangeend = [str rangeOfString:@" " options:NSLiteralSearch range:NSMakeRange(range.location,[str length] - range.location - 1)];
        if(rangeend.length) {
            return [str substringWithRange:NSMakeRange(range.location,rangeend.location - range.location)];
        }
        else
        {
            return [str substringFromIndex:range.location];
        }
    }
    else {
        return @"";
    }
}
查看更多
smile是对你的礼貌
4楼-- · 2019-01-08 12:04

This is what I'd do:

NSString *givenStringWithWhatYouNeed = @"What you want to look through";
NSArray *listOfWords = [givenStringWithWhatYouNeed componentsSeparatedByString:@" "];
for (NSString *word in listOfWords) {
    if ([[word substringWithRange:NSMakeRange(0, 1)]isEqualToString:@"#"]) {
        NSString *whatYouWant = [[word componentsSeparatedByString:@"#"]lastObject];
    }
}

Then you can do what you need with the whatYouWant instances. If you want to know which string it is (if it's the substring 1 or 2), check the index of of word string in the listOfWords array.

I hope this helps.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-08 12:07

A general and simple code to select all the words starting with "#" in a NSString is:

NSString * aString = @"This is the #substring1 and #subString2 ...";
NSMutableArray *selection=@[].mutableCopy;
while ([aString rangeOfString:@"#"].location != NSNotFound)
{
    aString = [aString substringFromIndex:[aString rangeOfString:@"#"].location +1];
    NSString *item=([aString rangeOfString:@" "].location != NSNotFound)?[aString substringToIndex:[aString rangeOfString:@" "].location]:aString;
    [selection addObject:item];
}

if you still need the original string you can do a copy. The inline conditional is used in case your selected item is the last word

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-08 12:09
[aString substringWithRange:NSMakeRange(13, 10)] 

would give you substring1

You can calculate the range using:

NSRange startRange = [aString rangeOfString:@"#"];
NSRange endRange = [original rangeOfString:@"1"];

NSRange searchRange = NSMakeRange(startRange.location , endRange.location);
[aString substringWithRange:searchRange] 

would give you substring1

Read more: Position of a character in a NSString or NSMutableString

and

http://iosdevelopertips.com/cocoa/nsrange-and-nsstring-objects.html

查看更多
等我变得足够好
7楼-- · 2019-01-08 12:14

Another simple solution:

NSRange hashtag = [aString rangeOfString:@"#"];
NSRange word = [[aString substringFromIndex:hashtag.location] rangeOfString@" "];
NSString *hashtagWord = [aString substringWithRange:NSMakeRange(hashtag.location, word.location)];
查看更多
登录 后发表回答