C# 字符串操作(部分重写,纯算法)
发布时间:2020-12-15 17:52:58 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 using System; using System.Collections.Generic; public static class c_str { public static int _find(this string str,string value) { return _
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 using System; using System.Collections.Generic; public static class c_str { public static int _find(this string str,string value) { return _find(str,value,0); } public static int _find(this string str,string value,int index) { if (str != null && value != null) { int i = index,len = str.Length,size = value.Length; if (i > -1 && len > 0 && size > 0) while (i < len) { int j = 0; if (i + size > len) break; while (j < size) { if (str[i + j] != value[j]) break; j++; } if (j == size) return i; i++; } } return -1; } public static int _rfind(this string str,string value) { if (str != null && value != null) { int len = str.Length; int size = value.Length; if (len > 0 && size > 0) { int i = len - size; while (i > -1) { int j = size - 1; while (j > -1) { if (str[i + j] != value[j]) break; j--; } if (j < 0) return i; i--; } } } return -1; } public static string[] _split(this string str,string value) { IList<int> finds = new List<int>(); int[] size = { str.Length,value.Length }; int i = _find(str,0); while (i > -1) { finds.Add(i); i = _find(str,i + size[1]); } size[1] = finds.Count; bool begin = false,end = false; if (size[1] > 0) { int min = size[1] - 1; if (finds[min] < size[0]) { end = true; size[1]++; } if (finds[0] > 0) { begin = true; size[1]++; } } int len = finds.Count; // strtok string[] buffer = new string[size[1]]; if (len > 0) { int x,y; len--; for (i = 0; i <= len; i++) { int j = i; x = finds[i]; if (i >= len && end) j++; if (begin && end || begin) j++; y = i < len ? finds[i + 1] : y = x; buffer[j] = _substr(str,x + 1,y); } if (end) { i = size[1] - 2; x = finds[len] + 1; y = size[0]; buffer[i] = _substr(str,x,y); } if (begin) { x = 0; i = len + 1; y = finds[0]; buffer[0] = _substr(str,y); } } return buffer; } public static string _substr(string str,int start,int end) { if (str != null && str != string.Empty) { int len = str.Length; if (end > len || start > len) return string.Empty; if (start > end || start < 0 || end < 0) return string.Empty; char[] buffer = new char[end - start]; for (int i = start; i < end; i++) buffer[i - start] = str[i]; return new string(buffer); } return string.Empty; } public static string _add(this string str,string value) { if (str == null || str == string.Empty) return value; if (value == null || value == string.Empty) return str; int len = str.Length,size = value.Length; char[] buffer = new char[len + size]; for (int i = 0; i < len; i++) buffer[i] = str[i]; for (int i = 0; i < size; i++) { int j = i + len; buffer[j] = value[i]; } return new string(buffer); } public static string _replace(this string str,string oldValue,string newValue) { if (str == null || oldValue == null) return str; int i = _find(str,oldValue); if ((i ) > (-1)) { int len = str.Length; string beige = _substr(str,i); string end = _substr(str,++i,len); str = _add(beige,newValue); str = _add(str,end); } return str; } public static string _sub_replace(this string str,string newValue) { int i = -1; while ((i = _find(str,oldValue)) > -1) str = _replace(str,oldValue,newValue); return str; } public static string _pad_left(this string str,int totalWidth,char paddingValue) { return _add(new string(paddingValue,totalWidth),str); } public static string _pad_right(this string str,char paddingValue) { return _add(str,new string(paddingValue,totalWidth)); } public static string _pad_left(this string str,int totalWidth) { return _pad_left(str,totalWidth,(char)32); } public static string _pad_right(this string str,int totalWidth) { return _pad_right(str,(char)32); } public static string _remove(this string str,int startIndex,int count) { if (str != null && str != string.Empty) { if (startIndex < 0) startIndex = str.Length + startIndex; string beige = _substr(str,startIndex); int x = startIndex + count; int y = str.Length; string end = _substr(str,y); return _add(beige,end); } return string.Empty; } public static string _remove(this string str,int startIndex) { if (str != null && str != string.Empty) { int len = str.Length - startIndex; return _remove(str,startIndex,len); } return string.Empty; } public static char[] _data(this string str) { int len = str != null ? str.Length : 0; char[] buffer = new char[len]; for (int i = 0; i < len; i++) buffer[i] = str[i]; return buffer; } public static char[] _c_str(this string str) { int len = str != null ? str.Length : 0; char[] buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = str[i]; return buffer; } public static int _size(this string str) { if (str != null && str != string.Empty) return str.Length; return 0; } public static bool _contains(this string str,value) > -1; } public static bool _equals(this string str,object obj) { if (str != null && obj != null) return str.GetHashCode() == obj.GetHashCode(); return false; } public static bool _equals(this string str,string value) { if (str != null && value != null) { int x = 0,y = 0; int len = str.Length; for (int i = 0; i < len; i++) x ^= str[i]; len = value.Length; for (int i = 0; i < len; i++) y ^= value[i]; return x == y; } return false; } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |