すくまりのメモ帳すくまりのメモ帳
← 記事一覧に戻る

【Unity】文字列stringの関数まとめ

2018-11-18
【Unity】文字列stringの関数まとめ

はじめに

この記事では文字列であるstring型の紹介と、string系の関数を紹介します。

文字列 string の使い方

文字列 "string" は 以下のように『string 変数 = "文字"』のように使います。" "が無いとエラーになります。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえお";
        Debug.Log(character);
 
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
出力結果
 
あいうえお

数字を文字列に変換する方法「ToString」

ハイスコアの表示など、"int" や "float" 等の数字をText で表示する際、string 型に変換する必要があります。

「数字. ToString()」で数字から文字列へ変換できます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        int num = 123;
        string character = num.ToString();
        Debug.Log(character);
 
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
実行結果
 
123

文字列を数字へ変換する方法「Parse」

 "123"のような文字列の数字を"int"型や"float"型へ変更したいときは、「int.Parse("123")」や「float.Parse("123")」のように記入します。

今回のコードの場合、character が整数ならtry{}の中のメソッドが実行され、それ以外ならエラーが発生するので例外処理のcatch{}が実行されます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "100";
 
        try
        {
            //整数の場合
            int num = int.Parse(character);
            Debug.Log("整数です:" + num);
        }
        catch
        {
            //小数の場合
            float num = float.Parse(character);
            Debug.Log("小数です:" + num);
        }
 
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
 
実行結果
 
//整数の場合
整数です:100
 
//小数の場合
小数です:100.23

文字数の取得方法「Length」

文字数をしゅとくするときは「変数.Length」のように記入します。全角も半角も1文字としてカウントされます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcde";
 
        int num = character.Length;
 
        Debug.Log("文字数:" + num);
 
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
 
実行結果
 
文字数:10

文字列の改行方法「\n」

表示する文字列を改行するときは、「"文字\n文字"」のように記入します。開発環境によっては¥nの場合もあります。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえお\nabcde";
 
        Debug.Log(character);
 
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
 
実行結果
 
あいうえお
abcde

文字内の該当文字を検索して先頭からの文字数を取得「IndexOf」

文字列内で検索したい文字を指定し、一致する文字がある場合は先頭から何文字目なのかを取得したい場合、「"文字". IndexOf ("検索したい文字")」のように記入します。

ただし、カウントは0から数えていきます。"あいう"のように複数文字を指定した場合、該当文字の頭文字"あ"の位置の番号が返されます。もし、"あいう"が複数セットある場合はカウントが小さいものが優先されます。

もし、○○文字以降の文字列を検索したい場合は、「IndexOf ("検索したい文字", 文字数)」のように入力すれば可能です。

該当文字がないときは―1を返します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcdeあいうえおabcde";
 
        int num = character.IndexOf("う");
        Debug.Log("1文字の場合:" + num);
 
        int num2 = character.IndexOf("うえ");
        Debug.Log("複数文字の場合:" + num2);
 
        int none = character.IndexOf("か");
        Debug.Log("該当文字が無いとき:" + none);
 
        int num3 = character.IndexOf("うえ", 10);
        Debug.Log("2回目の""以降のうえ:" + num3);
 
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
 
検索結果
 
1文字の場合:2
 
複数文字の場合:2
 
該当文字が無いとき:-1
 
2回目のあ以降のうえ:12

文字列の一部を抜き出す方法「Substring」

文字列内の〇〇番目から後ろ□□文字を取得したい場合、「"文字" . Substring (開始文字数, 文字数)」と記入します。

開始文字数は先頭文字が0からカウントします。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcdeあいうえおabcde";
 
        string part = character.Substring(1, 5);
 
        Debug.Log(part);
 
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
 
実行結果
 
いうえおa

文字列を分割する方法「Split」

文字列を指定した文字で分割したいときは「string[] 変数 = 文字. Split ('区切り文字')」のように記入します。

区切り文字を複数指定したいときは「string[] 変数 = 文字. Split ('区切り文字' ,  '区切り文字')」のように「,」で分割します。

区切り文字はシングルコーテーション「 '  '」で区切る点に注意してください。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcdeあいうえおabcde";
 
        string[] part = character.Split('う');
 
        Debug.Log("part[0]:" + part[0]);
        Debug.Log("part[1]:" + part[1]);
        Debug.Log("part[2]:" + part[2]);
 
    }
 
    // Update is called once per frame
    void Update () {
		
	}
}
 
実行結果
 
part[0]:あい
 
part[1]:えおabcdeあい
 
part[2]:えおabcde

文字列の一部を削除「Remove」

文字列の一部を削除したい場合は、「character.Remove(開始文字数 ,  文字数)」のように記入します。

これで、指定した文字数から、指定した文字数だけ文字列を削除できます。

開始文字数は0からカウントされることに注意してください。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcdeあいうえおabcde";
 
        string removed = character.Remove(1, 5);
 
        Debug.Log("removed:" + removed);
 
    }
 
    // Update is called once per frame
    void Update () {
		
	}
}
 
実行結果
 
removed:あbcdeあいうえおabcde

文字列の一部を別の文字に置き換える「Replace」

文字列の一部を別の文字に置き換える場合は、「character.Replace ("置き換えたい文字列", "置き換える文字列")」のように記入してください。

なお、置き換えたい文字列が複数ある場合は、全て置き換えられます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcdeあいうえおabcde";
 
        string replaced = character.Replace("abcde", "かきくけこ");
 
        Debug.Log("removed:" + replaced);
 
    }
 
    // Update is called once per frame
    void Update () {
		
	}
}
 
実行結果
 
removed:あいうえおかきくけこあいうえおかきくけこ

文字を大文字/小文字へ変換する方法「ToUpper/ToLower」

文字列を大文字にしたいときは「"文字" . ToUpper()」としてください。

文字列を小文字にしたいときは「"文字" . ToLower()」としてください。

もし、文字列に日本語が含まれていても、エラーにはならず、アルファベットにのみ適用されます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
 
        string character = "あいうえおabcde";
 
        string upper = character.ToUpper();
 
        Debug.Log("upper:" + upper);
 
    }
 
    // Update is called once per frame
    void Update () {
		
	}
}
 
実行結果
 
upper:あいうえおABCDE

ホームへ戻る