2012-03-17 75 views
0

我遇到了一個問題,我嘗試創建一個字符串,但該字符串(m)中的一個字母正在被刪除。我不明白髮生了什麼,你如何解決它?字符串放下字母?

這是代碼:

typeTo.text = [typeTo.text stringByReplacingOccurrencesOfString:@"random" withString:@"(random (1,10000000)/10000000)"]; 
NSLog(@"%@", typeTo.text); 

的NSLog打印:

rando 

感謝

+3

'typeTo.text'有沒有機會以'rando'開始? – mattjgalloway 2012-03-17 21:14:23

+0

'typeTo.text'的值是什麼? – edc1591 2012-03-17 21:14:46

+1

隨着日誌的確認,'typeTo.text'最初等於'rando'而不是'random'。所以你提供的代碼是正確的,但是這個字符串不包含任何'random'。把'NSLog(@「%@」,typeTo.text)放在''typeTo.text = [typeTo.text ...'之前''''''''''''''''''''''''''''''''不是'stringByReplacingOccurrencesOfString'的錯。 – sch 2012-03-17 21:26:49

回答

3

請檢查輸入的字符串...我只是測試這一點:

NSString *initialString = @"a random string is always random"; 
initialString = [initialString stringByReplacingOccurrencesOfString:@"random" withString:@"(random (1,10000000)/10000000)"]; 
NSLog(@"%@", initialString); 

它pri nted:

2012-03-17 21:15:41.276無題[39581:90​​3]一個(隨機 (1,10000000)/ 10000000)字符串總是(隨機(1,10000000)/ 10000000)

它工作正常,用(隨機(1,10000000)/ 10000000)替換所有出現的隨機。

+0

是的,我發現這個問題,我更換「m」之前,我取代「隨機」。謝謝 – 2012-03-18 15:01:16