如何截断 NSString?

发布于 2024-11-29 05:09:49 字数 465 浏览 1 评论 0原文

我查看了字符串格式化文档,但无法弄清楚具体如何执行此操作。

假设我有这样的刺

@"(01–05) 对神经系统的操作"

我想从中创建 2 个字符串,如下所示:

@"01-05" code> 和 @“神经系统操作”

我该怎么做?

以下是我查看的文档: http: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

I looked at the string formatting documents but couldn't figure out exactly how to do this.

Lets say I have a sting like this

@"(01–05) Operations on the nervous system"

I want to create 2 strings from this like so:

@"01-05" and @"Operations on the nervous system"

How can I do this?

Here are the docs I looked at: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

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

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

发布评论

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

评论(3

小…楫夜泊 2024-12-06 05:09:49

试一试。可能有点不对劲,我还没检查过是否有错别字。但既然你明白了,你就可以乱搞它了。

NSString * sourceString = @"(01–05) Operations on the nervous system";

NSString *string1 = [sourceString substringToIndex:6];
string1 = [string1 stringByReplacingOccurrencesOfString:@"(" withString:@""];

//string1 = 01-05

NSString *string2 =[sourceString substringFromIndex:7];

//string2 = Operations on the nervous system

Give this a shot. It might be off a bit, I havent checked for typos. But you can mess around with it now that you get the idea.

NSString * sourceString = @"(01–05) Operations on the nervous system";

NSString *string1 = [sourceString substringToIndex:6];
string1 = [string1 stringByReplacingOccurrencesOfString:@"(" withString:@""];

//string1 = 01-05

NSString *string2 =[sourceString substringFromIndex:7];

//string2 = Operations on the nervous system
死开点丶别碍眼 2024-12-06 05:09:49

如果您只想要字符“(”和“)”包含的第一个子字符串以及之后的任何内容,我建议您这样做:

NSString *original = @"(01–05) Operations on the nervous system";
NSString *firstPart = [NSString string];
NSString *secondPart = [NSString string];
NSScanner *scanner = [NSScanner scannerWithString:original];

[scanner scanUpToString:@"(" intoString:NULL];           // find first "("
if (![scanner isAtEnd]) {
    [scanner scanString:@"(" intoString:NULL];           // consume "("
    [scanner scanUpToString:@")" intoString:&firstPart]; // store characters up to the next ")"
    if (![scanner isAtEnd]) {
        [scanner scanString:@")" intoString:NULL];       // consume ")"

        // grab the rest of the string
        secondPart = [[scanner string] substringFromIndex:[scanner scanLocation]];
    }
}

当然,第二部分字符串前面仍然会有空格之类的东西,以获得摆脱那些你可以做一些事情:

secondPart = [secondPart stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];

使用 NSScanner 的优点是你不必对firstPart子字符串的开始和结束进行硬编码。

If you just want the first substring contained by the characters "(" and ")" and anything after that I'd recommend doing something like this:

NSString *original = @"(01–05) Operations on the nervous system";
NSString *firstPart = [NSString string];
NSString *secondPart = [NSString string];
NSScanner *scanner = [NSScanner scannerWithString:original];

[scanner scanUpToString:@"(" intoString:NULL];           // find first "("
if (![scanner isAtEnd]) {
    [scanner scanString:@"(" intoString:NULL];           // consume "("
    [scanner scanUpToString:@")" intoString:&firstPart]; // store characters up to the next ")"
    if (![scanner isAtEnd]) {
        [scanner scanString:@")" intoString:NULL];       // consume ")"

        // grab the rest of the string
        secondPart = [[scanner string] substringFromIndex:[scanner scanLocation]];
    }
}

Of course the secondPart string will still have spaces and whatnot at the front of it, to get rid of those you can do something along the lines of:

secondPart = [secondPart stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];

The advantage of using NSScanner is that you don't have to hard-code the start and end of the firstPart substring.

冬天的雪花 2024-12-06 05:09:49

NSString *theFirstStringSubString = [NSString substringFromIndex:1];

NSString *theFirstStringSecondSubstring = [theFirstStringSubString substringToIndex:6];

现在FirstStringSecondSubstring是01-05
另一个是同样的事情,但索引不同。请注意,这些是自动释放的字符串。如果你想保留它们,就保留它。

NSString *theFirstStringSubString = [NSString substringFromIndex:1];

NSString *theFirstStringSecondSubstring = [theFirstStringSubString substringToIndex:6];

Now theFirstStringSecondSubstring is 01-05
same thing for the other but at different indexes. Please note that these are strings that are autoreleased. If you want to keep them, retain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文