輕松進修C#的String類。本站提示廣大學習愛好者:(輕松進修C#的String類)文章只能為提供參考,不一定能成為您想要的結果。以下是輕松進修C#的String類正文
字符串是由零個或多個字符構成的無限序列,是簡直一切編程說話中可以完成的異常主要和有效的數據類型。在C#說話中,字符串是System.String類的一個援用類型,但與其他援用類型分歧的是,C#將字符串視為一個根本類型,可以聲明為一個常量,並可以直接賦值。因為C#中的字符串是由System,String類派生而來的援用對象,是以可使用String類的辦法來對字符串停止各類操作。上面經由過程幾個例子來說述String類的幾個主要辦法。
1、字符串的截取
字符串截取是經由過程Substring辦法完成的,它有兩種重載辦法,格局分離為:
(1)字符串1.Substring(整數n);將字符串1前n個長度的字符串截取失落,保存前面的字符串
(2)字符串1.Substring(整數n,整數m);保存從字符串1第n個長度開端數m個長度的字符串
兩種重載辦法都前往一個新的字符串。
例一:完成對字符串“0123456789”的截取
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string nums = "0123456789";
string newnums1;
string newnums2;
newnums1 = nums.Substring(5);//截取從索引5開端後的字符
newnums2 = nums.Substring(3,5);//截取從索引3開端數5個字符
Console.WriteLine(newnums1);
Console.WriteLine(newnums2);
Console.ReadLine();
}
}
}</span>
輸入的成果為:56789
34567
留意:字符串的索引是從0開端的,在應用Substring辦法的第二種重載時,整數n和整數m的和不要年夜於要截取的字符串的長度,不然會發生越出索引異常。
2、字符串的朋分
字符串的朋分是經由過程Split辦法完成的。經常使用的一種格局為:
字符串1.Split(字符串或字符數組)
經由過程Split辦法朋分字符串後將生成多個字符串,所以經由Split辦法朋分的前往值是一個字符串數組。
例二:完成對字符串“abcefgaabsbdeesdabc”的朋分
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str="abcefgaabsbdeesdabc";
Console.WriteLine("原字符串為{0}",str);
Console.WriteLine("經由過程單個字符e朋分後以下:");
string[] singleSplit = str.Split('e');//停止單個字符朋分的辦法
foreach (string outstr in singleSplit)
{
Console.WriteLine(outstr);
}
Console.WriteLine("經由過程多個字符e,b,s朋分後以下:");
string[] multiSplit = str.Split(new char[] {'e','b','s'});//停止多個字符朋分的辦法
foreach (string outstr in multiSplit)
{
Console.WriteLine(outstr);
}
Console.ReadLine();
}
}
}
</span>
輸入的成果為:

3、字符串的歸並
字符串的歸並經由過程“+”,Concat辦法和Join辦法來完成的。
(1)用“+”符號來銜接兩個字符串後構成一個新的字符串,格局為:字符串1+字符串2=字符串3。
(2)用Concat辦法銜接字符串的格局為:string.Concat(字符串1,字符串2,...,字符串n)。
(3)用Join辦法是將字符串數據歸並為一個新的字符串,格局為:string.Join(歸並後的分隔符,字符串數組)。
例三,完成對字符串str1和str2的歸並
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str1 = "abc";
string str2 = "ghj";
string[] array = {"123","456","789"};
string str3 = str1 + str2;
string str4 = string.Concat(str1,str2);
string str5 = string.Join("|",array);//將數組中元素歸並為一個新的字符串
Console.WriteLine(str3);
Console.WriteLine(str4);
Console.WriteLine(str5);
Console.ReadLine();
}
}
}</span>
輸入的成果為:abcghj
abcghj
123|456|789
4、字符串的調換
字符串的調換是經由過程WordStr辦法完成的,格局為:字符串.WordStr(要調換的原字符串,調換後的字符串);
例四,完成對字符串str的調換
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "abcadfaslfj";
string replacestr = str.WordStr("a","|");//用“|”調換“a”
Console.WriteLine(replacestr);
Console.ReadLine();
}
}
}</span>
輸入的成果為:|bc|df|slfj
5、字符串的拔出與填充
字符串的拔出是經由過程Insert辦法完成的,其格局為:字符串.Insert(拔出地位,拔出字串)
字符串的填充是經由過程PadRight辦法和PadLeft辦法完成的。
PadRight辦法是在字符串的開頭經由過程添加指定的反復字符填充字符串,格局為:字符串.PadRight(總長度)(以空格填充)和字符串.PadRight(總長度,要填充的字符)。
PadLeft辦法是在字符串的開首經由過程添加指定的反復字符填充字符串,格局為:字符串.PadLeft(總長度)(以空格填充)和字符串.PadLeft(總長度,要填充的字符)。
例5、完成對字符串str的拔出與填充
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "abcdefg";
string insertstr;
string padrightstr;
string padleftstr;
insertstr = str.Insert(5,"12345");
padrightstr = str.PadRight(10,'v');
padleftstr = str.PadLeft(10,'w');
Console.WriteLine(insertstr);
Console.WriteLine(padrightstr);
Console.WriteLine(padleftstr);
Console.ReadLine();
}
}
}</span>
輸入的成果為:

六,字符串的刪除
字符串的刪除是經由過程Remove辦法完成的,格局為:
(1)字符串.Remove(開端地位)
(2)字符串.Remove(開端地位,移除數)
個中,開端地位是指字符串的索引,是一個整數,且小於字符串的長度。第一種格局,是將字符串開端地位後的一切子子符刪除,而第二種是將從開端地位開端數到移除數地位的字符刪除。
例六,完成字符串str的刪除
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "0123456789";
string delstr1;
string delstr2;
delstr1 = str.Remove(6);//刪除字符串索引為6前面的字符
delstr2 = str.Remove(5,5);//刪除字符串索引自5開端後數5個長度的字符
Console.WriteLine(delstr1);
Console.WriteLine(delstr2);
Console.ReadLine();
}
}
}</span>
輸入的成果為:012345
01234
7、字符串的復制
字符串的復制是經由過程Copy辦法和CopyTo辦法完成的。若想把一個字符串復制到另外一個字符數組中,可使用String的靜態辦法Copy來完成。其格局為:string.Copy(要復制的字符串)。
CopyTo辦法可以完成Copy異樣的功效,然則功效更加豐碩,可以復制原字符串的一部門到一個字符數組中,其格局為:CopyTo(要復制的字符肇端地位,目的字符數組,目的數組中的開端寄存地位,要復制的字符個數)。
例七,完成字符串str的復制
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "This is a string";
string copystr;
copystr = string.Copy(str);
char[] newchar=new char[20];
str.CopyTo(5,newchar,0,11);
Console.WriteLine(copystr);
Console.WriteLine(newchar);
Console.ReadLine();
}
}
}</span>
輸入的成果為:This is a string
is a string
8、字符串的年夜小寫轉換
字符串年夜小寫轉換是經由過程String類的ToLower辦法和ToUpper辦法完成的,ToLower辦法是將字符串轉換為小寫情勢,ToUpper是將字符串轉換為年夜寫情勢。
例八,完成字符串str的年夜小寫轉換
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "This is a string";
string lowerstr;
string upperstr;
lowerstr = str.ToLower();
upperstr = str.ToUpper();
Console.WriteLine("小寫情勢:{0}",lowerstr);
Console.WriteLine("年夜寫情勢:{0}",upperstr);
Console.ReadLine();
}
}
}</span>
輸入的成果為:this is a string
THIS IS A STRING
9、字符串的查找
字符串的查找是經由過程IndexOf辦法和LastIndexOf辦法完成的。其格局為:
字符串.IndexOf(要查找的字符或字符串)
字符串.LastIndexOf(要查找的字符或字符串)
個中,IndexOf辦法是前往要查找的字符或字符串第一次在所要查找的字符串湧現的地位,LastIndexOf辦法是前往要查找的字符或字符串最初一次在所要查找的字符串中湧現的地位。IndexOf辦法和LastIndexOf辦法都前往一個整數,假如在所要查找的字符串內不包括要查找的字符或字符串則前往一個正數。
例九,完成字符串str的查找
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
string str = "This is a string";
int rh1 = str.IndexOf("i");
int rh2 = str.LastIndexOf("i");
if (rh1>=0)
{
Console.WriteLine("字符i在字符串str第一次湧現的地位是:{0}",rh1);
Console.WriteLine("字符i在字符串str最初一次湧現的地位是:{0}", rh2);
}
else
{
Console.WriteLine("字符i在字符串str未湧現");
}
Console.ReadLine();
}
}
}</span>
輸入的成果為:字符i在字符串str第一次湧現的地位是:2
字符i在字符串str最初一次湧現的地位是:13
10、字符串的追加
在應用System.String類中的辦法時,都要在內存中創立一個新的字符串對象,這就須要為該新對象分派新的空間。在須要對字符串履行反復修正的情形下,與創立新的String對象相干的體系開支便可能異常高。為懂得決這個成績,C#供給了一個類StringBuilder。
應用StringBuilder類時,起首要引入System.Text定名空間,然後經由過程new症結字對其停止初始化。StringBuilder類的辦法應用和String類的辦法應用是一樣的。
例10、完成對字符串str的追加
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
StringBuilder str =new StringBuilder( "Hellow World!");
Console.WriteLine("---Append---");
str.Append("What a beautiful day");
Console.WriteLine("追加後的字符串為:{0}",str);
Console.ReadLine();
}
}
}</span>
輸入的成果為:---Append---
追加後的字符串為:Hellow World! What a beautiful day
彌補:本義字符
本義字符具有特定的寄義,分歧於字符原本的意義的字符。在C#說話中,本義字符是指“\”,重要用來表現那些用普通字符不便利表現的掌握代碼。
關於本義字符的輸入C#說話有著特別的格局:
<span >using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(@"C:\Windows\system32");//第一種輸入格局就是在後面加@
Console.WriteLine("C:\\Windows\\system32");//第二種輸入格局就是將"\"改成"\\"
Console.ReadLine();
}
}
}
</span>
輸入的成果為:

以上就是C#的String類全體進修例題,很具體的進修教程,願望對年夜家的進修有所贊助。