C#學習筆記之字符串常用方法
ToUpper()/ToLower()
作用:將字符串中字符轉換為大寫/小寫字符,僅對字符有效,返回轉換后的字符串。
使用:字符串變量名.ToUpper() / 字符串變量名.ToLower()
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "abcDE123";
Console.WriteLine(s.ToUpper());
Console.WriteLine(s.ToLower());
Console.ReadKey();
}
}
}輸出結果:
ABCDE123
abcde123
Equals()
作用:比較兩字符串是否相同,是,則返回真,否則返回假。
使用:字符串1.Equals(字符串2)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s1, s2, s3;
s1 = "12345";
s2 = "12345";
s3 = "l2345";
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(s2.Equals(s3));
Console.ReadKey();
}
}
}s1與s2中內容均為"12345",而s3中首字符為英文小寫字母'l',s1、s2相同,s3與s1、s2不相同,則輸出結果為:
True
False
Split()
作用:根據所選擇的字符對字符串分割,返回字符串類型的數組。
使用:將分割的字符串.Split(用于分割的字符數組)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "111,22,3333.4,555";
char[] c = new char[2] { ',', '.' };
string[] sArray = s.Split(c);
foreach(string strIter in sArray)
{
Console.WriteLine(strIter);
}
Console.ReadKey();
}
}
}Split根據字符','與'.',將字符串s分割后依次存入字符串數組sArray,最后輸出結果為
111
22
3333
4
555
Substring()
作用:從某一下標開始,截取字符串,返回截取后的字符串。
使用:被截取的字符串.Substring(截取起點索引值[,截取字符串長度])
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "1234567";
Console.WriteLine(s.Substring(1));
Console.WriteLine(s.Substring(1,4));
Console.ReadKey();
}
}
}第一次截取從索引值為1的位置開始截取至字符串結束,第二次截取從索引值為1的位置開始截取4個字符長度的字符串。輸出結果如下:
234567
2345
IndexOf()/LastIndexOf()
作用:查找子字符串在字符串中第一次/最后一次出現位置索引,如果未找到,返回-1。
使用:字符串.IndexOf(查找的子字符串)/字符串.LastIndexOf(查找的子字符串)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "12345671234567";
Console.WriteLine(s.IndexOf("234"));
Console.WriteLine(s.LastIndexOf("234"));
Console.WriteLine(s.IndexOf("57"));
Console.ReadKey();
}
}
}第一次查找,找到了"234",返回第一次出現"234"時'2'的索引;第二次查找,找到了"234",返回最后一次出現"234"時'2'的索引;第三次查找,沒有找到"57",返回-1。最終結果:
1
8
-1
StartsWith()/EndsWith()
作用:判斷字符串是否以所查找的字符串開頭/結束,是,則返回True,否則返回False。
使用:字符串變量.StartsWith(子字符串)/字符串變量.EndsWith(子字符串)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "12345671234567";
Console.WriteLine(s.StartsWith("1234")); //s以"1234"為起始,返回True
Console.WriteLine(s.EndsWith("567")); //s以"567"結束,返回True
Console.WriteLine(s.StartsWith("57")); //s不以"57"為起始,返回False
Console.ReadKey();
}
}
}最終輸出結果為:
True
True
False
Replace()
作用:將指定字符串中子字符串s1更換為子字符串s2,返回更新后的字符串。
使用:更新字符串.Replace(s1,s2)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "12345671234567";
Console.WriteLine(s.Replace("1234","000")); //將s中所有的"1234"子字符串更換為"000"
Console.ReadKey();
}
}
}最終輸出為:
000567000567
此處可見,實際上Replace將字符串中全部匹配的子字符串都進行了更換。
Contains()
作用:判斷字符串中是否存在某一子字符串,是,則返回True,否則返回False。
使用:字符串變量.Contains(子字符串)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = "12345671234567";
Console.WriteLine(s.Contains("1234")); //s中存在"1234",返回True
Console.WriteLine(s.Contains("000")); //s中不存在"000",返回False
Console.ReadKey();
}
}
}輸出結果如下:
True
False
Trim()/TrimEnd()/TrimStart()
作用:去除字符串首尾/尾部/首部空格,返回去除空格后的字符串。
使用:字符串變量.Trim()/字符串變量.TrimEnd()/字符串變量.TrimStart()
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s = " string ";
Console.WriteLine("\"" + s.Trim() + "\""); //去除s首尾空字符
Console.WriteLine("\"" + s.TrimEnd() + "\""); //去除s尾部空字符
Console.WriteLine("\"" + s.TrimStart() + "\""); //去除s首部空字符
Console.ReadKey();
}
}
}輸出結果如下:
"string"
" string"
"string "
IsNullOrEmpty()
作用:判斷字符串是否為空字符串或者為null,是,則返回True,否則返回False。
注意,字符串為空字符串與為null不同??兆址?quot;")占有內存空間,null則不占有。
使用:string.IsNullOrEmpty(字符串變量) (此處使用方法與前面幾個函數不同)
使用實例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class Program
{
static void Main(string[] args)
{
string s1 = " string "; //字符串非空
string s2 = null; //字符串為null
string s3 = ""; //字符串為空字符串
Console.WriteLine(string.IsNullOrEmpty(s1));
Console.WriteLine(string.IsNullOrEmpty(s2));
Console.WriteLine(string.IsNullOrEmpty(s3));
Console.ReadKey();
}
}
}輸出結果如下:
False
True
True
總結
到此這篇關于C#學習筆記之字符串常用方法的文章就介紹到這了,更多相關C#字符串常用方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#中Abstract方法和Virtual方法的區(qū)別
這篇文章介紹了C#中Abstract方法和Virtual方法的區(qū)別,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

