0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列⇔値 の変換【備忘録】

Posted at

文字列 ⇐ 値

文字列を数値に変換するにはToString()メソッドで変換できます

    double a = 3.14159;
    
    // b = "3.14159"
    string b = a.ToString();

文字列 ⇒ 値

Parse() メソッドで変更することができます

    //例
    string a = "100"
    //a = 100
    int b = int.Parse(a);

TryParse()メソッドを使えば戻り値として 成功時 → true 失敗時 → false を取得できます

    //例
    string[] values = {"200", "NULL", "hoge", "100.45"};
    int ans;
    
    foreach (string value in values)
    {
        bool flag = int.TryParse(value, out ans);

        if (flag)
        {
            Console.WriteLine($"値は{ans}です");
        }
        else
        {
            Console.WriteLine("変換されませんでした");
        }
    }
    //結果↓
    //値は200です
    //変換されませんでした
    //変換されませんでした
    //変換されませんでした

↓参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?