简体   繁体   English

如何找出NSString中是否有“。”?

[英]How to find out if there is an “.” in an NSString?

Have got an 有一个

NSString *str = @"12345.6789"

and want to find out if there is that "." 并想知道是否存在“。” character inside of it. 里面的字符。 I'm afraid that there are ugly char-encoding issues when I would just try to match an @"." 恐怕我尝试匹配@“时,会遇到难看的字符编码问题。” against this? 反对这个? How would you do it to make sure it always finds a match if there is one? 如果有匹配项,您将如何做以确保始终找到匹配项?

I just need to know that there is a dot in there. 我只需要知道那里有一个点。 Everything else doesn't matter. 其他一切都没有关系。

You can use rangeOfString: message to get the range where your "." 您可以使用rangeOfString:消息获取“。”所在的范围。 is. 是。

The prototype is: 原型是:

- (NSRange)rangeOfString:(NSString *)aString

You can find more info about this message in: Mac Dev Center 您可以在以下位置找到有关此消息的更多信息: Mac Dev Center

There would be something like this: 会有这样的事情:

NSRange range;  
range = [yourstring rangeOfString:@"."]; 
NSLog(@"Position:%d", range.location);

If you need to, there is another message ( rangeOfString:options: ) where you can add some options like "Case sensitive" and so on. 如果需要,还有另一条消息(rangeOfString:options:),您可以在其中添加一些选项,例如“区分大小写”等。

If [str rangeOfString:@"."] returns anything else than {NSNotFound, 0} , the search string was found in the receiver. 如果[str rangeOfString:@"."]返回{NSNotFound, 0}以外的任何其他{NSNotFound, 0} ,则在接收器中找到搜索字符串。 There are no encoding issues as NSString takes care of encoding. 由于NSString负责编码,因此没有编码问题。 However, there might be issues if your str is user-provided and could contain a different decimal separator (eg, a comma). 但是,如果您的str是用户提供的,并且可能包含其他小数点分隔符(例如,逗号),则可能会出现问题。 But then, if str really comes from the user, many other things could go wrong with that comparison anyway. 但是,如果str确实来自用户,那么无论如何,这种比较可能都会出错。

To check . 去检查 。 symbol, it will be useful. 符号,将很有用。

if ([[str componentsSeparatedByString:@"."] count]>1) {
        NSLog(@"dot is there");
}else{
        NSLog(@"dot is not there");
}

If what you really want to do is determine whether the string represents a number with a fractional part, a better solution is to feed the string to a number formatter, then examine the number's doubleValue to see whether it has a fractional part. 如果您真正想做的是确定字符串是否代表带有小数部分的数字,那么更好的解决方案是将字符串提供给数字格式化程序,然后检查数字的doubleValue以查看其是否具有小数部分。

For the latter step, one way would be to use the modf function , which returns both the fractional part (directly) and the integral part (by reference). 对于后面的步骤,一种方法是使用modf函数 ,该函数同时返回小数部分(直接)和整数部分(通过引用)。 If the fractional part is greater than zero (or some appropriately small fraction below which you're willing to tolerate), then the number has a fractional part. 如果小数部分大于零(或您愿意容忍的某个较小的小数部分),则数字为小数部分。

The reason why this is better is because not everybody writes decimal fractions in the “12345.6789” format. 之所以更好,是因为并非每个人都以“ 12345.6789”格式写小数。 Some countries use a comma instead, and I'm sure that's not the only variation. 有些国家/地区改用逗号,但我敢肯定这不是唯一的变体。 Let the number formatter handle such cases for you. 让数字格式化程序为您处理这种情况。

I wrote a little method to make things a little more natural if you use this sort of thing a whole bunch in your project: 如果您在项目中使用了很多东西,我写了一种方法让事情变得更自然:

+(BOOL)seeIfString:(NSString*)thisString ContainsThis:(NSString*)containsThis
{
    NSRange textRange = [[thisString lowercaseString] rangeOfString:[containsThis lowercaseString]];

    if(textRange.location != NSNotFound)
        return YES;

    return NO;
}

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM