zoukankan      html  css  js  c++  java
  • string类型小结

    在C#中string字符串类型一直是比较常见,它是String的别名,我们可以根据自己的喜好来命名,下面总结一下它的常见方法:

    1.比较两个字符串的大小

    public static int Compare(string strA, string strB);

    参数:strA是要比较的第一个参数,strB是要比较的第二个参数; 返回值:小于零,strA小于strB;等于零,strA等于strB;大于零,strA大于strB。

    public static int Compare(string strA, string strB, bool ignoreCase);

    参数:strA是要比较的第一个参数,strB是要比较的第二个参数;ignoreCase:要在比较过程中忽略大小写,则为 true;否则为 false。返回值:小于零,strA小于strB;等于零,strA等于strB;大于零,strA大于strB。

    public static int Compare(string strA, int indexA, string strB, int indexB, int length);

    参数:strA是要比较的第一个参数,indexA是strA 中子字符串的位置;strB是要比较的第二个参数,indexB是strB 中子字符串的位置;length是 要比较的子字符串中字符的最大数量;返回值:小于零 ,strA 中的子字符串小于 strB 中的子字符串;等于零,strA 中的子字符串等于strB 中的子字符串;大于零,strA 中的子字符串大于 strB 中的子字符串。

    public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase);

    根据上面三个方法可以总结出这个方法是用来比较两个字符串中的字字符串,且不区分子字符串的大小;返回值同以上三个方法的返回值是一致的。

    示例演示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strA = "abc";
                string strB = "ABC";
    
                MyCompare(strA, strB);
                MyCompareNoCase(strA, strB);
                MyCompareIndex(strA, strB);
                MyCompareIndexNoCase(strA, strB);
                
                Console.ReadKey();
            }
    
            static void MyCompare(string strA, string strB)
            {
                int nReturnCode = String.Compare(strA, strB);
                if (nReturnCode < 0)
                {
                    Console.WriteLine("strA小于strB");
                }
                else if (nReturnCode > 0)
                {
                    Console.WriteLine("strA大于strB");
                }
                else
                {
                    Console.WriteLine("strA与strB相等");
                }
            }
            static void MyCompareNoCase(string strA, string strB)
            {
                int nReturnCode = String.Compare(strA, strB, true);
    
                if (nReturnCode < 0)
                {
                    Console.WriteLine("不区分大小写后,strA小于strB");
                }
                else if (nReturnCode > 0)
                {
                    Console.WriteLine("不区分大小写后,strA大于strB");
                }
                else
                {
                    Console.WriteLine("不区分大小写后,strA与strB相等");
                }
    
            }
    
            static void MyCompareIndex(string strA, string strB)
            {
                int nReturnCode = String.Compare(strA, 0, strB, 0, 2);
                if (nReturnCode < 0)
                {
                    Console.WriteLine("strA子字符串小于strB子字符串");
                }
                else if (nReturnCode > 0)
                {
                    Console.WriteLine("strA子字符串大于strB子字符串");
                }
                else
                {
                    Console.WriteLine("strA子字符串与strB子字符串相等");
                }
            }
    
            static void MyCompareIndexNoCase(string strA, string strB)
            {
                int nReturnCode = String.Compare(strA, 0, strB, 0, 2, true);
                if (nReturnCode < 0)
                {
                    Console.WriteLine("不区分大小写后,strA子字符串小于strB子字符串");
                }
                else if (nReturnCode > 0)
                {
                    Console.WriteLine("不区分大小写后,strA子字符串大于strB子字符串");
                }
                else
                {
                    Console.WriteLine("不区分大小写后,strA子字符串与strB子字符串相等");
                }
            }
        }
    }

    2.从指定位置删除字符串

    public string Remove(int startIndex);

    参数: startIndex开始删除字符串的初位置(从零开始);返回值:一个新的字符串,除了被删除的子字符串除外。

    public string Remove(int startIndex, int count);

    参数:startIndex开始删除字符串的初位置(从零开始),count:指定删除的字符串数量;返回值:一个新的字符串,除了被删除的子字符串除外。

    示例演示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strA = "123abc";
                string str1 = strA.Remove(1);
                string str2 = strA.Remove(1, 3);
                Console.WriteLine(str1);
                Console.WriteLine(str2);
              
                Console.ReadKey();
            }    
        }
    }

    3.字符串替换

    public string Replace(string oldValue, string newValue);

    参数:oldValue要被替换的字符串,newValue要替换出现的所有 oldValue 的字符串;返回值:等效于当前字符串(除了 oldValue 的所有实例都已替换为 newValue 外)的字符串。

    示例演示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str1 = "123";
                string str2 = "abc";
                string str3 = str1.Replace("12", str2);
                Console.WriteLine(str3);
                Console.ReadKey();
            }    
        }
    }

    4.字符串截取

     public string Substring(int startIndex);

    参数:startIndex是子字符串的起始字符位置(从零开始),返回值:从 startIndex开始时的子字符串。

    public string Substring(int startIndex, int length);

    参数:startIndex是子字符串的起始字符位置(从零开始),length需要截取的子字符串的个数;返回值:从 startIndex位置开始截取个数为length的子字符串。

    示例显示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str1 = "123456abcdefg";
                string str2 = str1.Substring(2);
                string str3 = str1.Substring(2, 5);
                Console.WriteLine(str2);
                Console.WriteLine(str3);
                Console.ReadKey();
            }    
        }
    }

    5.字符串分割

    (1).用字符串分割

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "aaajsbbbjscc";
                string[] sArray = Regex.Split(str, "js", RegexOptions.IgnoreCase);
                foreach(string str1 in sArray)
                {
                    Console.WriteLine(str1);
                }
                Console.ReadKey();
            }    
        }
    }

    (2).用多个字符来分割

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "aaajsbbbjscc";
                string[] sArray = str.Split(new char[2] { 'j', 's' });
                foreach(string str1 in sArray)
                {
                    Console.WriteLine(str1);
                }
                Console.ReadKey();
            }    
        }
    }

    (3).用单个字符来分割

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "aaajsbbbjscc";
                string[] sArray = str.Split('j');
                foreach(string str1 in sArray)
                {
                    Console.WriteLine(str1);
                }
                Console.ReadKey();
            }    
        }
    }
  • 相关阅读:
    中小型MIS开发之我见(二)具体实施(上)
    实现.NET应用程序的自动更新
    利用XML实现通用WEB报表打印(参考)
    几款开放源代码的软件测试工具介绍
    TextBox只输入数字
    HttpUtility 类 编码,解码字符串为Html字符串
    测试工具的选择和使用
    javascript技巧参考
    认识Web.config文件
    动态改变页面的CSS样式(收藏备用)
  • 原文地址:https://www.cnblogs.com/QingYiShouJiuRen/p/11357274.html
Copyright © 2011-2022 走看看