检查一个单词后面是否有单词(NSSTRING)

时间:2015-07-22 03:50:37

标签: ios objective-c

我有一个有趣的小问题。所以我需要创建一个if语句,它可以检测数组中的单词之后是否有单词

所以我的代码是

NSString*needle=words[index+1];

if (needle isEqualto @"") {
    //There is a word after @"string";
}

我不确定是否必须做类似

的事情
public class MainViewModel : INotifyPropertyChanged
{
    private Visibility _imageVisibility;
    public Visibility ImageVisibility
    {
        get { return _imageVisibility; }
        set { _imageVisibility = value; OnPropertyChanged("ImageVisibility"); }
    }

    private BitmapImage _imageSource;
    public BitmapImage ImageSource{...}

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler eventHandler = PropertyChanged;
        if (eventHandler != null)
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我该怎么办?

如何判断@“string”后面是否有单词?

我感谢所有的帮助!! :)

2 个答案:

答案 0 :(得分:0)

NSString *yourString = @"string stackoverflow word after string regex";
NSRange   yourStringRange = NSMakeRange(0, [yourString length]);
NSString *pattern = @"string\\s*([^\\s]*)";
NSError  *error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
NSArray* matches = [regex matchesInString:yourString options:0 range: yourStringRange];
for (NSTextCheckingResult* match in matches) {
    NSRange rangeForMatch = [match rangeAtIndex:1];
    NSLog(@"word after STRING: %@", [yourString substringWithRange:rangeForMatch]);
}

<强>输出:

word after STRING: stackoverflow
word after STRING: regex

答案 1 :(得分:-1)

方法componentsSeparatedByString:将按输入NSString分隔所有组件。

你的例子。 NSArray *words = [texfield.text componentsSeparatedByString:@" "];

将所有字符串分隔为" "。所以只需检查是否有

NSInteger afterIndex = index+1; if(afterIndex<words.count)如果是,您有NSString可用。