using UnityEngine; using System; using System.Collections; using System.Text; public static class ExtensionString { public static T ToEnum(this string str) { return (T)Enum.Parse(typeof(T), str); } public static string Remove(this string str, int startIndex, int endIndex, bool empty) { if (startIndex > endIndex) { throw new Exception(); } return str.Remove(startIndex, endIndex - startIndex + 1); } public static string ReplaceByLength(this string str, int index, int length, string newStr) { str = str.Remove(index, length); str = str.Insert(index, newStr); return str; } public static string Replace(this string str, int startIndex, int endIndex, string newStr) { if (startIndex > endIndex) { throw new Exception(); } str = str.Remove(startIndex, endIndex - startIndex + 1); str = str.Insert(startIndex, newStr); return str; } public static string Between(this string str, int startIndex, int endIndex) { if (startIndex > endIndex) { return ""; } else if (startIndex == endIndex) { return str[startIndex].ToString(); } else { return str.Substring(startIndex, endIndex - startIndex + 1); } } public static int GetBytes(this string str) { if (str.Equals(string.Empty)) return 0; int strlen = 0; ASCIIEncoding strData = new ASCIIEncoding(); //将字符串转换为ASCII编码的字节数字 byte[] strBytes = strData.GetBytes(str); for (int i = 0; i <= strBytes.Length - 1; i++) { if (strBytes[i] == 63) //中文都将编码为ASCII编码63,即"?"号 strlen++; strlen++; } return strlen; } }