本文介绍了从NSString中的某个字符获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我最初有一个NSString:

If I have an NSString that is initially:

"ABCDE*FGHI"

如何让它变成

"FGHI"

换句话说,从星号开始的所有内容都会保留。

In other words, everything from the asterisk onwards is kept.

同样,我如何将其变为:

Likewise, how would I turn it into:

"ABCDE"

(保留星号的所有内容)

(everything up to the asterisk is kept)

谢谢

推荐答案

NSString *myString = @"ABCDE*FGHI";
NSArray *myArray = [myString componentsSeparatedByString:@"*"];




  • myArray的密钥0将包含@ABCDE

  • 键1将包含@FGHI

  • 如果您想要多个字符作为分隔符,

    使用 componentsSeparatedByCharactersInSet

    If you want more than one character to be the separator,
    use componentsSeparatedByCharactersInSet:

    NSString *myString = @"ABCDE*FGHI-JKL";
    NSArray *myArray = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*-"]];
    




    • myArray的密钥0将包含@ABCDE

    • 键1将包含@FGHI

    • 键2将包含@JKL

    • 这篇关于从NSString中的某个字符获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:34