从@中删除字符串

发布于 2024-11-02 13:13:32 字数 234 浏览 2 评论 0原文

NSString *string=@"[email protected]";

现在我想从 @ 中删除导致“test1”的字符串的所有字符。 请建议我如何做到这一点。 提前致谢!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兲鉂ぱ嘚淚 2024-11-09 13:13:32

您可以使用该方法

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

,因此您的代码应如下所示。

string = [string stringByReplacingOccurrencesOfString:@"sdk.com" withString:@""];

编辑:

使用componentsSeparatedByString方法,

 NSArray* myArray = [string componentsSeparatedByString:@"@"];

现在你的数组恰好包含两个组件,

NSMutableString* myStringBefore = [myArray objectAtIndex:0];

如果你仍然想在字符串末尾添加@..

[myStringBefore appenString:@"@"];

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:@"@"];
新一帅帅 2024-11-09 13:13:32

您可以使用 substringToIndex

string = [ string substringToIndex:5 ] ;
// If @ location is subject to change, then first find it's location index and
// pass it.

You can use substringToIndex

string = [ string substringToIndex:5 ] ;
// If @ location is subject to change, then first find it's location index and
// pass it.
手心的温暖 2024-11-09 13:13:32
 NSMutableString *string1 = [NSMutableString stringWithString: @"[email protected]"];
 NSArray* arr = [string1 componentsSeparatedByString:@"@"];
[string1 deleteCharactersInRange: [string1 rangeOfString: [NSString stringWithFormat:@"@%@",[arr objectAtIndex:1]]]];
 NSMutableString *string1 = [NSMutableString stringWithString: @"[email protected]"];
 NSArray* arr = [string1 componentsSeparatedByString:@"@"];
[string1 deleteCharactersInRange: [string1 rangeOfString: [NSString stringWithFormat:@"@%@",[arr objectAtIndex:1]]]];
明月夜 2024-11-09 13:13:32
int range=[string rangeOfString:@"@"].location; 
NSLog(@"%d",range); 
NSLog(@"%@",[string substringToIndex:range]);
int range=[string rangeOfString:@"@"].location; 
NSLog(@"%d",range); 
NSLog(@"%@",[string substringToIndex:range]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文