Reputation: 11314
NSString *string=@"[email protected]";
Now I want to delete all the characters of the string from @ which result in "test1". Please suggest me how to do this. Thanks in advance!!
Upvotes: 0
Views: 116
Reputation: 11314
int range=[string rangeOfString:@"@"].location;
NSLog(@"%d",range);
NSLog(@"%@",[string substringToIndex:range]);
Upvotes: 0
Reputation: 31722
You could use the method
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
So your code should be like below.
string = [string stringByReplacingOccurrencesOfString:@"sdk.com" withString:@""];
EDITED:
Use componentsSeparatedByString
method,
NSArray* myArray = [string componentsSeparatedByString:@"@"];
Now your array contain exactly two component,
NSMutableString* myStringBefore = [myArray objectAtIndex:0];
if you still want @ at the end of your string ..
[myStringBefore appenString:@"@"];
Upvotes: 1
Reputation: 10312
NSMutableString *string1 = [NSMutableString stringWithString: @"[email protected]"];
NSArray* arr = [string1 componentsSeparatedByString:@"@"];
[string1 deleteCharactersInRange: [string1 rangeOfString: [NSString stringWithFormat:@"@%@",[arr objectAtIndex:1]]]];
Upvotes: 1
Reputation: 34625
You can use substringToIndex
string = [ string substringToIndex:5 ] ;
// If @ location is subject to change, then first find it's location index and
// pass it.
Upvotes: 1