如何将句子分成单词?

时间:2014-07-24 07:05:04

标签: ios objective-c nsstring nsarray

将句子拆分为单词而不使用" componentsSeparatedByString:"

我的话是"这是一个众所周知的简单"

我这样写separtedWord=[noteTextView.text componentsSeparatedByString: @" "]; 但我希望不使用componentsSeparatedByString。请帮助我

2 个答案:

答案 0 :(得分:1)

我写了以下逻辑。创建了两个这样的属性:

@property(nonatomic,strong) NSMutableArray *strings;
@property(nonatomic,strong) NSMutableString *tempString;

写出这样的业务逻辑:

NSString *sampleString = @"This is a well known simple";
self.tempString = [[NSMutableString alloc] init];
self.strings = [[NSMutableArray alloc] init];

for( int i = 0; i < sampleString.length; i++ )
{
    unichar currentChar = [sampleString characterAtIndex:i];
    NSString *character = [NSString stringWithCharacters:&currentChar length:1];

    if( currentChar != ' ' )
    {
        [self.tempString appendString:character];

        if( i == sampleString.length - 1 )
        {
            [self addString:self.tempString];
        }
    }
    else
    {
        [self addString:self.tempString];
        [self.tempString setString:@""];
    }
}

NSLog(@"Array Of String = %@",self.strings);

- (void)addString:(NSString *)string
{
    [self.strings addObject:[NSString stringWithString:string]];
}

2014-07-24 15:23:22.306 ViemoPlayer [1834:70b] String of String =(     这个,     是,     一个,     好,     众所周知,     简单 )

希望这有帮助。

答案 1 :(得分:0)

将NSString转换为char数组。 循环遍历数组和if语句,并使用

继续将charArray中的charecters附加到本地NSMutableString中
for(int i =0;i<[myCharArray count];i++){
 NSMutableString *teststring;
[teststring appendString:[myCharArray objectAtIndex : i]];

if([myCharArray objectAtIndex] == " "){
     NSLog(teststring);
     teststring = @""; //emptying the testString when we get a space
 }
}

应该这样做