0

Подскажите, как вырезать из строки определённый текст? Например: NSString * String1 = @"12345result=678910"; , вот мне нужно в NSString *String2 = @"678910";

размер после result= - динамический - может достигать от 1-го до 40-ка символов.

4 ответа 4

1

Популярный вопрос. Я тоже влезу

NSString *res = @"122345result=678910";
NSRange position = [res rangeOfString:@"result="];
if(position.location != NSNotFound) {
    NSString *newString = [res substringFromIndex:position.location+position.length];
}
0

использовал такую конструкцию:

NSArray  *data = [@"abc xyz http://www.abc.com aaa bbb ccc" componentsSeparatedByString:@" "];

for(NSString* str in data)
{
    if([NSURLConnection canHandleRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]])
        NSLog(@"%@",[[NSString alloc ] initWithFormat:@"Found a URL: %@",str]);
}
1
  • что то ответ не подходит к вопросу 14 июл 2016 в 18:17
0

В Вашем случае самое простое:

NSString *String1 = @"12345result=678910";
NSString *String2 = [String1 stringByReplacingOccurrencesOfString:@"12345result=" withString:@""];
0

Так будет немного универсальней:

NSString *string1 = @"12345result=678910";
NSArray *components = [string1 componentsSeparatedByString: @"="];
NSString *result = nil;
if( components.count == 2 )
        result = components[1];

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.