欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#學(xué)習(xí)筆記之字符串常用方法

 更新時間:2024年01月30日 11:22:16   作者:Southern?Young  
在C#中字符串是用于表示文本的一系列字符,它可以是字符、單詞 或用雙引號引起來的長段落,下面這篇文章主要給大家介紹了關(guān)于C#學(xué)習(xí)筆記之字符串常用方法的相關(guān)資料,需要的朋友可以參考下

ToUpper()/ToLower()

作用:將字符串中字符轉(zhuǎn)換為大寫/小寫字符,僅對字符有效,返回轉(zhuǎn)換后的字符串。

使用:字符串變量名.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();
        }
    }
}

輸出結(jié)果:

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中內(nèi)容均為"12345",而s3中首字符為英文小寫字母'l',s1、s2相同,s3與s1、s2不相同,則輸出結(jié)果為:

True

False

Split()

作用:根據(jù)所選擇的字符對字符串分割,返回字符串類型的數(shù)組。

使用:將分割的字符串.Split(用于分割的字符數(shù)組)

使用實例如下:

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根據(jù)字符','與'.',將字符串s分割后依次存入字符串?dāng)?shù)組sArray,最后輸出結(jié)果為

111

22

3333

4

555

Substring()

作用:從某一下標(biāo)開始,截取字符串,返回截取后的字符串。

使用:被截取的字符串.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的位置開始截取至字符串結(jié)束,第二次截取從索引值為1的位置開始截取4個字符長度的字符串。輸出結(jié)果如下:

234567

2345

IndexOf()/LastIndexOf()

作用:查找子字符串在字符串中第一次/最后一次出現(xiàn)位置索引,如果未找到,返回-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",返回第一次出現(xiàn)"234"時'2'的索引;第二次查找,找到了"234",返回最后一次出現(xiàn)"234"時'2'的索引;第三次查找,沒有找到"57",返回-1。最終結(jié)果:

1

8

-1

StartsWith()/EndsWith()

作用:判斷字符串是否以所查找的字符串開頭/結(jié)束,是,則返回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"結(jié)束,返回True
            Console.WriteLine(s.StartsWith("57"));  //s不以"57"為起始,返回False
            Console.ReadKey();
        }
    }
}

最終輸出結(jié)果為:

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();
        }
    }
}

輸出結(jié)果如下:

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();
        }
    }
}

輸出結(jié)果如下:

"string"
"   string"
"string   "

IsNullOrEmpty()

作用:判斷字符串是否為空字符串或者為null,是,則返回True,否則返回False。

注意,字符串為空字符串與為null不同??兆址?quot;")占有內(nèi)存空間,null則不占有。

使用:string.IsNullOrEmpty(字符串變量)        (此處使用方法與前面幾個函數(shù)不同)

使用實例如下:

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();
        }
    }
}

輸出結(jié)果如下:

False
True
True

總結(jié) 

到此這篇關(guān)于C#學(xué)習(xí)筆記之字符串常用方法的文章就介紹到這了,更多相關(guān)C#字符串常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# WebApi Get請求方式傳遞實體參數(shù)的方法示例

    C# WebApi Get請求方式傳遞實體參數(shù)的方法示例

    這篇文章主要給大家介紹了關(guān)于C# WebApi Get請求方式傳遞實體參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法

    C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法

    工作和生活中經(jīng)常可以看到一些程序猿,寫代碼的時候只關(guān)注代碼的邏輯性,而不考慮運行效率,其實這對大多數(shù)程序猿來說都是沒有問題的,不過作為一只有理想的CodeMonkey,我還是希望給大家分享一些性能優(yōu)化心得
    2017-08-08
  • C#下使用XmlDocument操作XML詳解

    C#下使用XmlDocument操作XML詳解

    本文詳細講解了C#使用XmlDocument操作XML的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#遍歷系統(tǒng)進程的方法

    C#遍歷系統(tǒng)進程的方法

    這篇文章主要介紹了C#遍歷系統(tǒng)進程的方法,涉及C#底層操作獲取系統(tǒng)信息與硬件信息的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • c# 顏色選擇控件的實現(xiàn)代碼

    c# 顏色選擇控件的實現(xiàn)代碼

    這篇文章主要介紹了c# 顏色選擇控件的實現(xiàn)代碼,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 一文搞懂C#實現(xiàn)讀寫文本文件中的數(shù)據(jù)

    一文搞懂C#實現(xiàn)讀寫文本文件中的數(shù)據(jù)

    這篇文章重點給大家介紹C#實現(xiàn)讀寫文本文件中的數(shù)據(jù)的一些知識,讀取.txt文件數(shù)據(jù)的實例代碼及寫入讀取過程完整代碼,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • C# winform分頁查詢的實現(xiàn)示例

    C# winform分頁查詢的實現(xiàn)示例

    這篇文章主要介紹了C# winform分頁查詢的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 詳解如何在C#/.NET Core中使用責(zé)任鏈模式

    詳解如何在C#/.NET Core中使用責(zé)任鏈模式

    這篇文章主要介紹了詳解如何在C#/.NET Core中使用責(zé)任鏈模式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果

    Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#中Abstract方法和Virtual方法的區(qū)別

    C#中Abstract方法和Virtual方法的區(qū)別

    這篇文章介紹了C#中Abstract方法和Virtual方法的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評論