将文本表示为一系列 Unicode 字符。
Unity 将 .Net System.String
类用于字符串。请参阅 Microsoft MSDN 中
有关字符串
的文档,了解更多详细信息。
注意:在 c# 中,string
是 System.String
的别名。这意味着,
您可以在代码中使用 string
或 String
(如果已将 using System
添加到脚本顶部)。
注意:在 Javascript 中,字符串由 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); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.