Skip to content

【Unity】文字列を他の型に変換する拡張メソッド

License

Notifications You must be signed in to change notification settings

baba-s/Kogane.StringParseExtensionMethods

Repository files navigation

Kogane String Parse Extension Methods

文字列を他の型に変換する拡張メソッド

使用例

using Kogane;
using UnityEngine;

public class Example : MonoBehaviour
{
    private void Awake()
    {
        // 文字列を以下の型に変換する拡張メソッド
        // sbyte, byte, char, short, ushort, int, uint, long, ulong
        // float, double, decimal, DateTime, bool, enum

        var str = "1";

        int  r1 = str.ToInt();              // int に変換
        int? r2 = str.ToIntOrNull();        // int に変換。変換できない場合は null を取得
        int  r3 = str.ToIntOrDefault();     // int に変換。変換できない場合は既定値を取得
        int  r4 = str.ToIntOrDefault( 25 ); // int に変換。変換できない場合は引数に渡した値を取得
        bool r5 = str.IsInt();              // int に変換できる場合 true
    }
}

About

【Unity】文字列を他の型に変換する拡張メソッド

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages