我想从PlayerPrefs.GetString获取第三和第四个字母(" String") 和Parse到int。 例如;
string playerLevelstr = PlayerPrefs.GetString("Player")[2] + PlayerPrefs.GetString("Player")[3];
//My Player string is "0012000000" but when I plus third and fourth letter, playerLevelstr should be "12" but it is "96".
int playerLevelint = int.Parse(playerLevelstr);
答案 0 :(得分:3)
字符串上的索引器返回import UIKit
class ViewController: UIViewController {
var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.loginButton = UIButton()
self.loginButton.frame = CGRectMake(10, 40, self.view.bounds.width - 20, 40)
self.loginButton.setTitle("Login", forState: UIControlState.Normal)
self.loginButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
self.view.addSubview(self.loginButton)
self.loginButton.addTarget(self, action: "initLogin:", forControlEvents:.TouchUpInside)
}
func initLogin(button: UIButton)
{
println("Logging in...")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
。
如果你在char
上一起使用+运算符,它基本上对两个char
进行整数运算。
有关详细信息,请参阅this question。
虽然这意味着你应该得到99('1'是49,'2'是50')而不是96.但也许那是一端或另一端的错字?
无论如何,您应该将char
转换为char
s(string
)或使用.ToString()
上的Substring
功能代替。不要忘记你的长度/空检查!
答案 1 :(得分:1)
此代码string
返回PlayerPrefs.GetString("Player")[2]
,当您将其添加到另一个char
时,正在转换为int
(字符的ASCII值)。
请改为:
char