C#以及lua获取字符串字符个数

it2022-05-05  127

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using System.Text; public class Testinput : MonoBehaviour { // Use this for initialization void Start () { string str = "老外1a"; int count = Encoding.Default.GetByteCount(str);//一个汉字 三个字节 一个字母一个 byte[] bytes = Encoding.Default.GetBytes(str); int size = 0; int wantCount = 0;//实际长度 一个汉字两个字节 一个字母一个 for (int i = 1; i <= bytes.Length; i++) { size = (int)(bytes[i - 1]); if (size <= 128) { wantCount = wantCount + 3; } else { wantCount = wantCount + 2; } } Debug.Log(wantCount/3); } }

lua

--返回当前字符实际占用的字符数 一个字母代表一个字符 一个汉字代表两个字符(3) function PnlModifyArmyNameVM:SubStringGetByteCount(str, index) local curByte = string.byte(str, index) local byteCount = 1; if curByte == nil then byteCount = 0 elseif curByte > 0 and curByte <= 127 then --字母 byteCount = 3 else byteCount = 2 end return byteCount; end

 


最新回复(0)