C# 字符串操作(部分重写,纯算法)

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

重写了一些.Net String提供的函数的功能 不过命名并不代表完全一样哦。

_find = 寻找文本

_rfind  倒找文本

_split  分割文本

_substr  取子字符串

_sub_replace  替换子文本

_replace  替换文本

_add strcat 链接字符串

_pad_left 左填充文本

_pad_right 右填充文本

_remove 删除字符串

_c_str 到C字符串

_data 到C+字符串

_size 取文本尺寸

_contains 文本是否存在

_equals 文本是否相同

    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, value, 0);  
            while (i > -1)  
            {  
                finds.Add(i);  
                i = _find(str, value, 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, x, 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, 0, 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 oldValue, 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, int totalWidth, 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, totalWidth, (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, 0, startIndex);  
                int x = startIndex + count;  
                int y = str.Length;  
                string end = _substr(str, x, 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, string value)  
        {  
            return _find(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;  
        }  
    }  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:jquery砸金蛋 高仿阿里旅游砸金蛋

下一篇: php实现连续ping远程服务器脚本