「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

【UnityC#】文字列操作まとめ

自分用に文字列操作のまとめのようなもの作ってみました。

[ 文字列操作まとめ ]

文字列の連結

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abc" + "DEF";

        Debug.Log(StringTest);
    }
}
abcDEF

文字列の改行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abc" + "\n" + "DEF";

        Debug.Log(StringTest);
    }
}
abc
DEF

文字列を比較

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
     {
        string StringTest1 = "abc";
        string StringTest2 = "DEF";

        Debug.Log(StringTest1.Equals(StringTest2));
    }
}
False

文字列の一部を消去

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.Remove(1, 3));
    }
}
aEF

文字列の一部を置換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.Replace("abc", "123"));
    }
}
123DEF

文字列の一部を取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.Substring(1, 3));
    }
}
bcD

文字列の長さ(文字数)を取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.Length);
    }
}
6

文字列が特定の文字を含むか取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.Contains("bcDE"));
    }
}
True

特定の文字が先頭から何文字目にあるか取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.IndexOf("c"));
    }
}
2

特定の文字が後ろから何文字目にあるか取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcDEF";

        Debug.Log(StringTest.LastIndexOf("c"));
    }
}
2

数字を文字列に変換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        int IntTest = 12345;

        Debug.Log(IntTest.ToString());
    }
}
12345

文字列を整数に変換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "12345";

        Debug.Log(int.Parse(StringTest));
    }
}
12345

文字列を少数に変換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "12345";

        Debug.Log(float.Parse(StringTest));
    }
}
12345

文字列を小文字に変換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "ABCDEF";

        Debug.Log(StringTest.ToLower());
    }
}
abcdef

文字列を大文字に変換

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "abcdef";

        Debug.Log(StringTest.ToUpper());
    }
}
ABCDEF

文字列を分割

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        string StringTest = "A_B_C_D_E";

        string[] AryStringTest = StringTest.Split("_");

        for(int i = 0; i < AryStringTest.Length; i++)
        {
            Debug.Log(AryStringTest[i]);
        }
    }
}
A
B
C
D
E