using System; using System.Text; using System.Text.RegularExpressions; using UnityEngine; namespace GFGGame { public class StringUtil { private static int[] secPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4327, 4086, 4390, 4558, 4684, 4925, 5249, 9999 }; private static char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 't', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z' }; /// /// 设置指定文本的颜色 /// /// /// /// public static string GetColorText(string fmt, string color) { string str = string.Format("[color={0}]{1}[/color]", color, fmt); return str; } public static char GetPinYin(string str) { if (Convert.ToChar(str[0]) >= 0 && Convert.ToChar(str[0]) < 256) return str[0]; else return GetFirstLetter(str[0].ToString()); } //根据汉字拼音排序得到首字母 private static char GetFirstLetter(string str) { string posValue = StringToPos(str); for (int i = 0; i < secPosValue.Length - 1; i++) { if (int.Parse(posValue) >= secPosValue[i] && int.Parse(posValue) < secPosValue[i + 1]) { return firstLetter[i]; } } return 'z'; } //获取一个字符的区位码 private static string StringToPos(string value) { string posValue = string.Empty; byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(value); string lowCode = (bytes[0] - 160).ToString(); //取出低字节编码内容(两位16进制) if (lowCode.Length == 1) lowCode = "0" + lowCode; string hightCode = (bytes[1] - 160).ToString();//取出高字节编码内容(两位16进制) if (hightCode.Length == 1) hightCode = "0" + hightCode; posValue += (lowCode + hightCode);//加入到字符串中, return posValue; } /// /// 获取下载地址的文件名, 包含后缀 /// /// /// public static string GetUrlFileName(string url) { try { // 创建一个 Uri 对象 Uri uri = new Uri(url); // 获取 URL 的路径部分 string path = uri.AbsolutePath; // 从路径中提取文件名 string fileName = System.IO.Path.GetFileName(path); // // 获取不包含扩展名的文件名 // string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fileName); return fileName; } catch { //增加容错 return url; } } } }