Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.
Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.
CerrarPor alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.
CerrarRepresenta texto como una serie de caracteres Unicode.
Unity usa la clase .Net System.String
para cadenas. Ver la documentación Microsoft MSDN
para Strings
para mas detalles.
Nota: en c# string
es un alias para System.String
. Esto significa que
puedes usar cualquiera de los dos string
o String
in tu código( Si tienes agregado using System
en la parte superior de su script .)
Nota: En Javascript las cadenas son representadas usando String
que se debe utilizar
en su código script de Unity.
Éstos son algunos de los usos básicos de la clase 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); } }
Este ejemplo muestra como puedes examinar la clase String y ver qué métodos contiene.
// 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); } } }