2010-10-17 41 views
1

我需要的是一個已經在範圍內的角色。字符串的類型是NSMuttableString。 例如,我有一串「52.648」,我需要知道的是一個「。」。符號已經在字符串中。我該怎麼做?是已經在範圍內的角色?

回答

6

您可以使用NSString的的rangeOfString消息:

NSRange rng = [string rangeOfString:@"."]; 
if (rng.location != NSNotFound) 
    // "." is in the string at position rng.location 
+0

只是一個補充。我用這個,甚至有一堆NSLogs確認正確的數據NSNotFound沒有按預期工作。下面的鏈接有一個解決方案,如果你有這個問題。 – Rudiger 2010-10-18 01:40:56

+0

@Rudiger謝謝你。我檢查了鏈接的答案,並投票贊成:) – RedBlueThing 2010-10-18 01:47:30

1

下面的表達式爲真,當且僅當myChar是在MyString中:

[myString rangeOfString: [NSString stringWithFormat: @"%c", myChar] 
       options: NULL].location != NSNotFound 
0

正如除了其他的答案,其是正確的。我遇到了使用NSNotFound的問題。不知道是否它的錯誤,但使用range.length> 0按預期工作。我發佈了它here