从 NSString 中删除“@”和“:”之间的文本,同时保留其后的文本
Removing text from NSString between "@" and ":" while keeping the text after it
所以我正在阅读来自推特机器人的最新推文并将其分配给一个字符串,但有时它会直接向用户发送推文。这是一个可能看起来像的例子。
NSString tweet = @("@user hey heres my message: BLah blah with symbols!");
//part I want to keep is: " BLah blah with symbols!"
//or it could end up being
NSString tweet = @("@otheruser my msg is: Wow heres some more blah: and a second colon");
//part I want to keep is: " Wow heres some more blah: and a second colon"
我想始终删除与用户对话的第一部分,同时将消息保留在最后。有太多不同的消息无法使用 "stringByReplacingOccurrencesOfString"
我不想使用来自推特 API 的 "exlude-replies",因为这个机器人非常受欢迎,并且需要获取最多 100 个,因为 "count" 在 [=11] 之前应用=]
知道我该怎么做吗?我认为这与正则表达式有关,但我以前从未使用过它们,也没有能够让它们按照我想要的方式工作。我非常感谢熟悉正则表达式的任何人的帮助
编辑:此外,如果正则表达式不适用于这种情况,我接受对限制的解释以防止出现这种情况:)
我能想到的最简单的解决方案是创建一个 NSMutable 数组,方法是使用 NSString 函数 componentsSeparatedBy:@":"
,然后简单地删除第一个元素。
NSMutableArray *tweetArray = [[NSMutableArray alloc] initWithArray: [tweet componentsSeparatedByString:@":"]];
[tweetArray removeObjectAtIndex:0];
之后随机出现冒号的问题可以通过重新拼接来解决。
tweet = [tweetArray componentsJoinedByString:@":"];
编辑:修复用户指出的错误'maddy'
您必须将这段代码放在 if 语句中,这样它就不会在通常带有冒号的推文中执行。但是,您可以使用它始终以 @user 开头的事实。
if ([tweet characterAtIndex:0] == '@' && [tweet characterAtIndex:1] != ' '){
NSMutableArray *tweetArray = [[NSMutableArray alloc] initWithArray: [tweet componentsSeparatedByString:@":"]];
[tweetArray removeObjectAtIndex:0];
tweet = [tweetArray componentsJoinedByString:@":"];
}
你也可以用这个。
NSString *myString = @"@username is me: by using this as sample text";
NSRange range = [myString rangeOfString:@":"];
NSString *newString= [myString substringFromIndex:range.location];
NSLog(@"New String is : %@", newString);
所以我正在阅读来自推特机器人的最新推文并将其分配给一个字符串,但有时它会直接向用户发送推文。这是一个可能看起来像的例子。
NSString tweet = @("@user hey heres my message: BLah blah with symbols!");
//part I want to keep is: " BLah blah with symbols!"
//or it could end up being
NSString tweet = @("@otheruser my msg is: Wow heres some more blah: and a second colon");
//part I want to keep is: " Wow heres some more blah: and a second colon"
我想始终删除与用户对话的第一部分,同时将消息保留在最后。有太多不同的消息无法使用 "stringByReplacingOccurrencesOfString"
我不想使用来自推特 API 的 "exlude-replies",因为这个机器人非常受欢迎,并且需要获取最多 100 个,因为 "count" 在 [=11] 之前应用=]
知道我该怎么做吗?我认为这与正则表达式有关,但我以前从未使用过它们,也没有能够让它们按照我想要的方式工作。我非常感谢熟悉正则表达式的任何人的帮助
编辑:此外,如果正则表达式不适用于这种情况,我接受对限制的解释以防止出现这种情况:)
我能想到的最简单的解决方案是创建一个 NSMutable 数组,方法是使用 NSString 函数 componentsSeparatedBy:@":"
,然后简单地删除第一个元素。
NSMutableArray *tweetArray = [[NSMutableArray alloc] initWithArray: [tweet componentsSeparatedByString:@":"]];
[tweetArray removeObjectAtIndex:0];
之后随机出现冒号的问题可以通过重新拼接来解决。
tweet = [tweetArray componentsJoinedByString:@":"];
编辑:修复用户指出的错误'maddy'
您必须将这段代码放在 if 语句中,这样它就不会在通常带有冒号的推文中执行。但是,您可以使用它始终以 @user 开头的事实。
if ([tweet characterAtIndex:0] == '@' && [tweet characterAtIndex:1] != ' '){
NSMutableArray *tweetArray = [[NSMutableArray alloc] initWithArray: [tweet componentsSeparatedByString:@":"]];
[tweetArray removeObjectAtIndex:0];
tweet = [tweetArray componentsJoinedByString:@":"];
}
你也可以用这个。
NSString *myString = @"@username is me: by using this as sample text";
NSRange range = [myString rangeOfString:@":"];
NSString *newString= [myString substringFromIndex:range.location];
NSLog(@"New String is : %@", newString);