iphone remove next string - leave rest of string after particular occurrence of string

In objective c how to Remove text after a string occurrence.

for example i have to remove a text after occurrence of text 'good'

'iphone is good but..' here i have to remove the but text in the end so the text will be now 'iphone is good'

Answers


Try this:-

  NSArray *array = [string componentsSeperatedBy:@"good"];
  NSString *requiredString = [array objectAtIndex:0];

Try with below code

NSString *str_good = @"iphone is good but...";
NSRange range = [str_good rangeOfString:@"good"];
str_good = [str_good substringToIndex:range.location+range.length];

NSString * a = @"iphone is good but..";
NSRange match = [a rangeOfString:@"good"];
NSString * b = [a substringToIndex:match.location+match.length];

If you want to remove rest of the string after a particular occurrence of "but", you can get the range of "but" and trim the original string down

NSString * test = [NSString stringWithString:@"iphone is good but rest of string"];
NSRange range = [test rangeOfString:@"but"];
if (range.length > 0)   {
    NSString *adjusted = [test substringToIndex:range.location];
    NSLog(@"result %@", adjusted);
}

EDIT We can assume that the search does not want to cut of "butter is yellow", and can change the range to include " but"

NSRange range = [test rangeOfString:@" but"];

NSArray *array = [string componentsSeparatedByString:stringToSearch];
NSString *requiredString;
if ([array count] > 0) {
    requiredString = [[array objectAtIndex:0] stringByAppendingString:stringToSearch];
}

Need Your Help

Extract a QString from xml tag

xml regex string xpath qt4

I have this resulted QString from an http request, What I need to do is to extract only the string "一致" which is inside the tag

C++ iterator for custom template class

c++ templates iterator

I have nested iterator in my custom stack template class.

Clear previous Activities from the widget

android android-intent android-widget

First i load my application which starts activity A then i go to Activity B. Then press my home button and load Widget. Clicking the button in the widget loads an Activity XXXX which is a class ins...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.