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

スクリプト言語

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

フィードバック

ありがとうございます

この度はドキュメントの品質向上のためにご意見・ご要望をお寄せいただき、誠にありがとうございます。頂いた内容をドキュメントチームで確認し、必要に応じて修正を致します。

閉じる

送信に失敗しました

なんらかのエラーが発生したため送信が出来ませんでした。しばらく経ってから<a>もう一度送信</a>してください。ドキュメントの品質向上のために時間を割いて頂き誠にありがとうございます。

閉じる

キャンセル

マニュアルに切り替える

説明

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

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

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

#pragma strict

function Start () { var s : String = "hello"; Debug.Log(s); // prints hello s = String.Format("{0} {1}", s, "world"); Debug.Log(s); // prints hello world 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 helloworld

//Debug.Log(s.CharAt(1)); // CharAt not supported, produces compiler error // instead use array syntax below Debug.Log(s[1]); // prints e var c : char = 'x'[0]; // slight odd JS way of specifying a char // 'x' is a string and [0] means first character Debug.Log(s.IndexOf(c)); // prints -1 (s does not contain an x) var i : int = 42; s = i.ToString(); Debug.Log(s); // prints 42

s = "-43"; i = int.Parse(s); Debug.Log(i); // prints -43 var f : float = 3.14159265358979f; s = f.ToString(); Debug.Log(s); // prints 3.141593 (which is an approximation) s = "-7.14159265358979"; f = float.Parse(s); Debug.Log(f); // prints -7.141593 (which is an approximation) }
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 クラスについて明らかにしたうえ、含まれるメソッドを確認する 方法を示します。

// Javascript
#pragma strict

import System; import System.Reflection;

function Start () {

var t : Type = 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. for (var mi : MethodInfo in t.GetMethods()) { var s : System.String = System.String.Format("{0} {1} (", mi.ReturnType, mi.Name); var pars : ParameterInfo[] = mi.GetParameters(); for (var j : int = 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); } }
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このインスタンスの文字数を取得します(読み取り専用)