字符串操作>静态串String

静态串String>字符串操作>比较字符串

Compare是一个静态方法,返回值大于零,strA大于strB,等于零, strA等于strB,小于零,strA小于strB。

String.Compare方法比较字符串
using System;
using System.Collections.Generic;
using System.Text;

namespace Compare
{
    
class Program
    {
        
static void Main(string[] args)
        {
            System.String str 
= "这是用COMPARE方法";
            System.String str2 
= "这是用compare方法";
            
int i = String.Compare(str, str2);
            Console.WriteLine(
"使用Compare(string strA, string strB)方法返回的结果");
            DisplayResult(i);
            i 
= String.Compare(str, str2,true);
            Console.WriteLine(
"使用Compare(string strA, string strB, bool ignoreCase)方法返回的结果");
            DisplayResult(i);
            i 
= String.Compare(str, str2,StringComparison.CurrentCulture);
            Console.WriteLine(
"使用Compare(string strA, string strB, StringComparison comparisonType)方法返回的结果");
            DisplayResult(i);
            i 
= String.Compare(str, str2,true,new System.Globalization.CultureInfo("zh-cn"));
            Console.WriteLine(
"使用Compare(string strA, string strB, StringComparison comparisonType)方法返回的结果");
            DisplayResult(i);
            i 
= String.Compare(str,0,str2,0,5);
            Console.WriteLine(
"使用Compare(string strA, int indexA, string strB, int indexB, int length);方法返回的结果");
            DisplayResult(i);
            Console.ReadLine();
        }
        
static void DisplayResult(int i)
        {
            
if (i < 0)
            {
                Console.WriteLine(
"Str大于Str2");
            }
            
else if (i == 0)
            {
                Console.WriteLine(
"Str等于Str2");
            }
            
else if (i > 0)
            {
                Console.WriteLine(
"Str大于Str2");
            }
         }
    }
}


如果使用== 或者!=操作符进行字符串比较,实际上就是在调用Equals方法。

Equals方法
using System;
using System.Collections.Generic;
using System.Text;

namespace Equals
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string strA = "这是将要比较的字符串一";
            
string strB = "这是将要比较的字符串二";
            
bool equalsResult;
            equalsResult 
= String.Equals(strA, strB);
            DisplayResult(equalsResult);
            equalsResult 
= strA.Equals(strB);
            DisplayResult(equalsResult);
            equalsResult 
= strA.Equals(strB, StringComparison.Ordinal);
            DisplayResult(equalsResult);
            equalsResult 
= String.Equals(strA, strB, StringComparison.Ordinal);
            DisplayResult(equalsResult);

        }
        
static void DisplayResult(bool bl)
        {
            
if (bl)
                Console.WriteLine(
"这两个字符串是相等的");
            
else if (bl == false)
            {
                Console.WriteLine(
"这两个字符串不相等");
            }
        }
    }
}


CompareTo方法的比较结果与Compare相同

CompareTo方法
using System;
using System.Collections.Generic;
using System.Text;

namespace CompareTo
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string strA = "这是要比较的字符串一";
            
string strB = "这是要比较的字符串二";
            
int comparetoResult=strA.CompareTo(strB);
            DisplayResult(comparetoResult);
            Console.ReadLine();
        }
        
static void DisplayResult(int i)
        {
            
if (i < 0)
            {
                Console.WriteLine(
"Str大于Str2");
            }
            
else if (i == 0)
            {
                Console.WriteLine(
"Str等于Str2");
            }
            
else if (i > 0)
            {
                Console.WriteLine(
"Str大于Str2");
            }
        }
    }
}


静态串String>字符串操作>定位字符和子串

代码
using System;
using System.Collections.Generic;
using System.Text;

namespace IndexOf
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "++这是一个综合使用定位子串方法的C#示例++";
            Console.WriteLine(str);
            Console.WriteLine(
"从字符串出滤除掉++字符串");
            
//使用TrimStart和TrimEnd截取指定字符数组的字符
            string tempstr =str.TrimStart("++".ToCharArray()).TrimEnd("++".ToCharArray());            
            Console.WriteLine(
"使用TrimStart和TrimEnd方法的实现结果为:{0}",tempstr);            
            
//使用Substring方法截取指定数组的字符。
            tempstr = str.Substring(0,str.LastIndexOfAny("++".ToCharArray())-1);
            tempstr 
= tempstr.Substring(tempstr.LastIndexOfAny("++".ToCharArray())+1);
            Console.WriteLine(
"使用Substring的实现结果为:{0}",tempstr);
            Console.WriteLine(
"使用StartWith与EndWith方法");
            
//判断指定的字符串++是否位于字符串str的开头,如果是,则返回True,否则,返回False
            if(str.StartsWith("++"))
            {
                Console.WriteLine(
"字符串以子串++作为开头");
            }
            
//判断指定的字符串++是否位于字符串str的结尾,如果是,则返回True,否则,返回False
            if (str.EndsWith("++"))
            {
                Console.WriteLine(
"字符串以子串++作为结尾");
            }
            Console.WriteLine(
"str.IndexOf");
            
//获取指定的字符+在字符串str中的位置。如果存在,则返回在字符串中的索引
            int i = str.IndexOf("+");
            
if (i >= 0)
            {
                Console.WriteLine(
"+字符存在于字符串str中,索引位置是{0}",i);
            }
            
else
            {
                Console.WriteLine(
"+字符不存在于字符串str中");
            }
            
//获取指定的字符数组++在字符串str中的位置。如果存在,则返回在字符串中的索引
            Console.WriteLine("str.IndexOfAny");
             i 
= str.IndexOfAny("++".ToCharArray(),0,str.Length-1);
            
if (i >= 0)
            {
                Console.WriteLine(
"++字符存在于字符串str中,索引位置是{0}", i);
            }
            
else
            {
                Console.WriteLine(
"++字符不存在于字符串str中");
            }
            
//获取指定的字符+在字符串str中的位置。如果存在,则返回在字符串中的最后位置的索引
            Console.WriteLine("str.LastIndexOf");
             i 
= str.LastIndexOf("+");
            
if (i >= 0)
            {
                Console.WriteLine(
"+字符存在于字符串str中,最后的索引位置是{0}", i);
            }
            
else
            {
                Console.WriteLine(
"+字符不存在于字符串str中");
            }
            
//获取指定的字符数组++在字符串str中的位置。如果存在,则返回在字符串中的最后位置的索引
            Console.WriteLine("str.LastIndexOfAny");
             i 
= str.LastIndexOfAny("++".ToCharArray(),0);
            
if (i >= 0)
            {
                Console.WriteLine(
"++字符存在于字符串str中,最后的索引位置是{0}", i);
            }
            
else
            {
                Console.WriteLine(
"++字符不存在于字符串str中");
            }
            Console.ReadLine();
        }
    }
}


静态串String>字符串操作>格式化字符串

Program.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace FormatString
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
double d = 12.345;
            
int i = 12;
            
int x = 6;
            Console.WriteLine(
string.Format("将Double类型的值显示为货币形式为:{0:C}",d));
            Console.WriteLine(
string.Format("将Double类型的值显示为十进制形式为:{0:D}", i));
            Console.WriteLine(
string.Format("今天的日期是_短日期格式形示:{0:d}",DateTime.Now));
            Console.WriteLine(
string.Format("今天的日期是_长日期格式形示:{0:D}", DateTime.Now));
            Console.WriteLine(
string.Format("今天的日期是_完整日期格式形示:{0:f}", DateTime.Now));
            
//使用自定义的格式化字符串,将传入参数6*5,如果格式字符指定为X6,则6*6.
            Console.WriteLine(String.Format(new CustomerFormat(), "{0} 使用自定义的B16格式定义为{1:X5}"new object[] { x, x }));
            Console.ReadLine();
        }
    }
}


CustomerFormat.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace FormatString
{
    
/// <summary>
    
/// 自定义格式化字符的示例
    
/// </summary>
    class CustomerFormat:IFormatProvider,ICustomFormatter
    {
        
#region IFormatProvider 成员
        
//这个方法将被String.Format调用,以获取一个ICustomFormatter的实例来处理格式化。
        public object GetFormat(Type formatType)
        {
            
//如果类型为ICustomFormatter。
            if (formatType == typeof(ICustomFormatter))
            {
                
return this;
            }
            
else
            {
                
return null;
            }
        }
        
#endregion
        
#region ICustomFormatter 成员
        
//String.Format方法获取到ICustomeFormatter之后,将为每个参数调用Format方法。
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            
if (format == null)
            {
                
//如果没有指定格式化提供者,则不使用格式输出。
                return string.Format("{0}", arg);
            }
            
//如果格式字符串不是以X为开头。
            if (!format.StartsWith("X"))
            {
                
//如果format不是一个己定义的自定义格式字符,那么就使用IFormattable的ToString()的格式化支持。
                if (arg is IFormattable)
                {
                    
//直接调用参数的格式化输出,并指定foramt,和formatProvider.
                    return ((IFormattable)arg).ToString(format, formatProvider);
                }
                
//如果args对象不支持IFormattable,调用Object的Tostring.不做任何格式化操作
                else if (arg != null)
                {
                    
return arg.ToString();
                }
            }
            
//如果格式字符是以X字符开头。
            format = format.Trim(new char[] { 'X' });
            
int b = Convert.ToInt32(format);
            
int c = Convert.ToInt32(arg);
            
//输出格式字符。
            return "[自定义格式化例子" + c*b+"]";                     
        }
        
#endregion
    }
}


静态串String>字符串操作>连接字符串和分割字符串

连接
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string s1 = "第一个串";
            
string s2 = "第二个串";
            
string s3 = "第三个串";
            Console.WriteLine(
"用System.String的Concat方法:"+string.Concat(s1, s2, s3));
            Console.WriteLine(
"用+操作符:" + string.Concat(s1, s2, s3));
            Console.ReadLine();
        }
    }
}
分割
using System;
using System.Collections.Generic;
using System.Text;

namespace Contract
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "Hello,World";
            Console.WriteLine(
"分割前的字符串为:{0}", str);
            
char[] sepa = new char[] { ',' };
            
string[] strs=str.Split(sepa);
            Console.WriteLine(
"分割后的字符串为");
            
foreach (string s in strs)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
    }
}


静态串String>字符串操作>插入和填充字符串

Insert方法
using System;
using System.Collections.Generic;
using System.Text;

namespace Insert
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "Hello,World!";
            
//Insert方法可在字符串的任意位置插入任意的字符,两个参数,第一个是要插入的索引位置,第二个是要插入的字符串
            string str1 = str.Insert(6"Our ");
            Console.WriteLine(str1);
            Console.ReadLine();
        }
    }
}


PadRight,PadLeft方法
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string strLeft = "前导字符串填充!";
            
//在字符串的前面填充+字符,使其总长度达到20个
            Console.WriteLine(strLeft.PadLeft(20'+'));
            
string strRight = "后置字符串填充!";
            Console.WriteLine(strRight.PadRight(
20'+'));
            Console.ReadLine();

        }
    }
}


静态串String>字符串操作>删除和剪切字符串 

Remove方法
using System;
using System.Collections.Generic;
using System.Text;

namespace Remove
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "演示Remove功能";
            
//Remove方法指定要删除的字符长度,第一个参数是索引位置,第二个参数是长度
            Console.WriteLine(str.Remove(26));
            Console.ReadLine();

        }
    }
}


剪切字符串
using System;
using System.Collections.Generic;
using System.Text;

namespace TrimDemo
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string[] stringArray = GetStringArray();
            Console.WriteLine(
"需要执行处理的字符串为:{0}", String.Concat(stringArray));
            
for (int i = 0; i <= stringArray.GetUpperBound(0); i++)
            {
                stringArray[i] 
= stringArray[i].Trim();
            }
            Console.WriteLine(
"经过Trim()后的字符串为:{0}"string.Concat(stringArray));
            
for (int i = 0; i <= stringArray.GetUpperBound(0); i++)
            {
                stringArray[i] 
= stringArray[i].Trim("".ToCharArray());
            }
            Console.WriteLine(
"去掉字符串数组中的你字后是:{0}"string.Concat(stringArray));
            stringArray 
= GetStringArray();
            Console.WriteLine(
"使用TrimStart和TrimEnd方法处理字符串中的前导与后置空格");
            
for (int i = 0; i <= stringArray.GetUpperBound(0); i++)
            {
                stringArray[i] 
= stringArray[i].TrimStart(null).TrimEnd(null);
            }
            Console.WriteLine(
"使用TrimStart和TrimEnd方法处理后的结果是:{0}"string.Concat(stringArray));
            Console.ReadLine();
        }
        
public static string[] GetStringArray()
        {
            
string[] StringArray = { " 你好  ""   你可否告诉我   ""  你的  ""  名字是你  " };
            
return StringArray;
        }
    }
}


静态串String>字符串操作>复制字符串 

Copy和CopyTo方法
using System;
using System.Collections.Generic;
using System.Text;

namespace copyCopyTo
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string sourcestr = "这是源字符串";
            
//使用Copy方法,这时产生了一个新的串,Str2并不是引用str1指向的串,而是产生了一个新的副本。
            string destinationstr=String.Copy(sourcestr);
            Console.WriteLine(
"使用Copy方法调用的结果:{0}", destinationstr);
            
//------------------------------------------------
            char[] destination =  { 'T''h''e'' ''i''n''i''t''i''a''l'' ',
                
'a''r''r''a''y' };

            Console.WriteLine(destination);
            
//使用CopyTo方法,将指定的源字符串复制到char数组中
            sourcestr.CopyTo(0, destination, 4, sourcestr.Length-1);
            Console.WriteLine(
"下面是调用CopyTo方法后的调用结果");
            Console.WriteLine(destination);
            Console.ReadLine();
       }
    }
}


静态串String>字符串操作>替换字符串

Replace方法
using System;
using System.Collections.Generic;
using System.Text;

namespace Replace
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "这是要被替换的源字符串";
            Console.WriteLine(str);
            
string str2=str.Replace("要被替换的源字符串""己被替换的字符串");
            Console.WriteLine(str2);
            Console.ReadLine();
        }
    }
}


静态串String>字符串操作>更改大小写

代码
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "Hello,Sam";
            
string str2 = str.ToUpper();
            Console.WriteLine(
"转换为大写:{0}", str2);
            
string str3 = str.ToLower();
            Console.WriteLine(
"转换为小写:{0}", str3);
            Console.ReadLine();
        }
    }
}



合乎自然而生生不息。。。
原文地址:https://www.cnblogs.com/samwu/p/1846372.html