Unity C# 字串擷取
透過C#程式碼擷取字串中的某部分例如「abcd123efg」提取出「123」的效果
使用C# string.Remove(移除起點,移除數量)
正文開始
1.開一個新場景
2.在場景中新增一個text
3.設定Text的 text為:「abcdefg5/5test」(我們要擷取出5/5)
4.接下來新增一段程式碼RemoveTest
5.編輯RemoveTest(整個複製貼上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RemoveTest : MonoBehaviour
{
public Text testUI;
public string info;
void Start()
{
//取得Text的字串
info = testUI.text;
Debug.Log("字串長度為"+info.Length);
//第一次切割,移除前面七個字元abcdefg
info = info.Remove(0, 7);
Debug.Log("切割字串長度為" + info.Length);
//第二次切割,移除後面四個字元(起始點為自創長度-4)
info = info.Remove(info.Length - 4, 4);
Debug.Log("切割結果: "+info);
}
}
6.把程式碼拖曳到MainCamera上
7.把剛剛創建好的Text拖曳到 Test UI欄位上並按下執行觀看結果(在Log)
8.切割完成
==========================Done!!!!