Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从中删除字符串@_Iphone_Objective C_Nsstring - Fatal编程技术网

Iphone 从中删除字符串@

Iphone 从中删除字符串@,iphone,objective-c,nsstring,Iphone,Objective C,Nsstring,现在我想从@中删除字符串的所有字符,这将导致“test1”。 请告诉我怎么做。 提前谢谢 你可以使用这个方法 NSString *string=@"test1@sdk.com"; 因此,您的代码应该如下所示 - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

现在我想从@中删除字符串的所有字符,这将导致“test1”。 请告诉我怎么做。
提前谢谢

你可以使用这个方法

NSString *string=@"test1@sdk.com";
因此,您的代码应该如下所示

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement
编辑:

使用字符串分离的组件

string = [string stringByReplacingOccurrencesOfString:@"sdk.com" withString:@""];
现在您的数组正好包含两个组件

 NSArray* myArray = [string componentsSeparatedByString:@"@"];
如果你还想在字符串末尾加上@

NSMutableString* myStringBefore = [myArray objectAtIndex:0];

你可以用这个方法

NSString *string=@"test1@sdk.com";
因此,您的代码应该如下所示

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement
编辑:

使用字符串分离的组件

string = [string stringByReplacingOccurrencesOfString:@"sdk.com" withString:@""];
现在您的数组正好包含两个组件

 NSArray* myArray = [string componentsSeparatedByString:@"@"];
如果你还想在字符串末尾加上@

NSMutableString* myStringBefore = [myArray objectAtIndex:0];
你可以用

你可以用


这个字符串只是我必须做的一个例子testabc@gmail.com.So解决方案需要是generalI找到的解决方案int range=[string rangeOfString:@“@”]。位置;NSLog(@“%d”,范围);NSLog(@“%@,[string substringToIndex:range]);这个字符串只是我必须做的一个例子testabc@gmail.com.So解决方案需要是generalI找到的解决方案int range=[string rangeOfString:@“@”]。位置;NSLog(@“%d”,范围);NSLog(@“%@,[string substringToIndex:range]);为什么不干脆
[[string组件由string:@“@”]objectAtIndex:0分离]
?不需要可变字符串。为什么不简单地
[[theString components被字符串分隔:@“@”]objectAtIndex:0]
?不需要可变字符串。