C#学习笔记之字符串常用方法

 更新时间:2024年01月30日 11:22:16   作者:Southern Young  
在C#中字符串是用于表示文本的一系列字符,它可以是字符、单词 或用双引号引起来的长段落,下面这篇文章主要给大家介绍了关于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不同。空字符串("")占有内存空间,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#字符串常用方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# WebApi Get请求方式传递实体参数的方法示例

    C# WebApi Get请求方式传递实体参数的方法示例

    这篇文章主要给大家介绍了关于C# WebApi Get请求方式传递实体参数的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • C#程序员应该养成的程序性能优化写法

    C#程序员应该养成的程序性能优化写法

    工作和生活中经常可以看到一些程序猿,写代码的时候只关注代码的逻辑性,而不考虑运行效率,其实这对大多数程序猿来说都是没有问题的,不过作为一只有理想的CodeMonkey,我还是希望给大家分享一些性能优化心得
    2017-08-08
  • C#下使用XmlDocument操作XML详解

    C#下使用XmlDocument操作XML详解

    本文详细讲解了C#使用XmlDocument操作XML的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#遍历系统进程的方法

    C#遍历系统进程的方法

    这篇文章主要介绍了C#遍历系统进程的方法,涉及C#底层操作获取系统信息与硬件信息的相关技巧,需要的朋友可以参考下
    2015-05-05
  • c# 颜色选择控件的实现代码

    c# 颜色选择控件的实现代码

    这篇文章主要介绍了c# 颜色选择控件的实现代码,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • 一文搞懂C#实现读写文本文件中的数据

    一文搞懂C#实现读写文本文件中的数据

    这篇文章重点给大家介绍C#实现读写文本文件中的数据的一些知识,读取.txt文件数据的实例代码及写入读取过程完整代码,感兴趣的朋友跟随小编一起看看吧
    2021-06-06
  • C# winform分页查询的实现示例

    C# winform分页查询的实现示例

    这篇文章主要介绍了C# winform分页查询的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 详解如何在C#/.NET Core中使用责任链模式

    详解如何在C#/.NET Core中使用责任链模式

    这篇文章主要介绍了详解如何在C#/.NET Core中使用责任链模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Unity3D实现物体旋转缩放移动效果

    Unity3D实现物体旋转缩放移动效果

    这篇文章主要为大家详细介绍了Unity3D实现物体旋转缩放移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#中Abstract方法和Virtual方法的区别

    C#中Abstract方法和Virtual方法的区别

    这篇文章介绍了C#中Abstract方法和Virtual方法的区别,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论