Version: 5.3 (switch to 5.4b)
言語English
  • C#
  • JS

スクリプト言語

好きな言語を選択してください。選択した言語でスクリプトコードが表示されます。

マニュアルに切り替える

説明

テキストを連続した Unicode 文字として表現します。

Unity は文字列として .Net System.String クラスを使用します。詳細については Microsoft MSDN ドキュメントの Strings を参照してください。

注意: C# で string は System.String のエイリアスです。つまり string または String のどちらもコードで使用できます。( using System をコード冒頭で追加することが前提。) 注意: Javascript で string は String により表現されるため、 これを Unity スクリプトコードで使用すべきです。 String クラスの基本的な使用方法を次に示します。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { // prints hello string s = "hello"; Debug.Log(s);

// prints hello world s = string.Format("{0} {1}", s, "world"); Debug.Log(s);

// prints helloworld s = string.Concat("hello", "world"); Debug.Log(s);

// prints HELLOWORLD s = s.ToUpper(); Debug.Log(s);

// prints helloworld s = s.ToLower(); Debug.Log(s);

// prints 'e' Debug.Log(s[1]);

// prints 42 int i = 42; s = i.ToString(); Debug.Log(s);

// prints -43 s = "-43"; i = int.Parse(s); Debug.Log(i);

// prints 3.141593 (an approximation) float f = 3.14159265359F; s = f.ToString(); Debug.Log(s);

// prints -7.141593 (an approximation) s = "-7.14159265358979"; f = float.Parse(s); Debug.Log(f); } }

このサンプルで String クラスについて明らかにしたうえ、含まれるメソッドを確認する 方法を示します。

using System;
using System.Reflection;
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { Type t = typeof(System.String); // Iterate over all the methods from the System.String class and display // return type and parameters. // This reveals all the things you can do with a String. foreach (MethodInfo mi in t.GetMethods()) { System.String s = System.String.Format("{0} {1} (", mi.ReturnType, mi.Name); ParameterInfo[] pars = mi.GetParameters(); for (int j = 0; j < pars.Length; j++) { s = String.Concat(s, String.Format("{0}{1}", pars[j].ParameterType, ((j == pars.Length - 1) ? "" : ", "))); } s = String.Concat(s, ")"); Debug.Log(s); } } }

変数

Empty空の文字列を表現します(読み取り専用)
Lengthこのインスタンスの文字数を取得します(読み取り専用)