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#字符串常用方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Jquery+Ajax+Json+存储过程实现高效分页

    Jquery+Ajax+Json+存储过程实现高效分页

    这篇文章主要介绍Jquery+Ajax+Json+存储过程实现分页,需要的朋友可以参考下
    2015-08-08
  • 在C#中优化JPEG压缩级别和文件大小方式

    在C#中优化JPEG压缩级别和文件大小方式

    文章介绍了如何在C#中优化JPEG压缩级别和文件大小,通过使用文件菜单加载图像文件并选择不同的压缩级别,程序将图像保存为临时文件并显示生成的图像和文件大小,关键方法SaveJpg使用给定的压缩指数保存JPG文件,并通过GetEncoderInfo获取编码器信息
    2025-01-01
  • 在C#中新手易犯的典型缺陷

    在C#中新手易犯的典型缺陷

    这篇文章的主要内容是关于C#那些新手易犯的典型缺陷,只有对有可能犯的错误进行总结才能做得更好,需要的朋友可以参考下
    2015-07-07
  • C#巧用DateTime预设可选的日期范围(如本年度、本季度、本月等)

    C#巧用DateTime预设可选的日期范围(如本年度、本季度、本月等)

    这篇文章主要介绍了C#巧用DateTime预设可选的日期范围,如本年度、本季度、本月等,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • C#实现Log4Net日志分类和自动维护实例

    C#实现Log4Net日志分类和自动维护实例

    这篇文章主要介绍了C#实现Log4Net日志分类和自动维护,实例讲述了Log4Net日志分类和自动维护的实现方法,并提供了完整的实例供大家参考学习,需要的朋友可以参考下
    2014-10-10
  • 详解C# 匿名对象(匿名类型)、var、动态类型 dynamic

    详解C# 匿名对象(匿名类型)、var、动态类型 dynamic

    随着C#的发展,该语言内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑。下面通过本文给大家分享C# 匿名对象(匿名类型)、var、动态类型 dynamic,需要的的朋友参考下吧
    2017-09-09
  • .NET操作PDF的完整指南

    .NET操作PDF的完整指南

    在.NET 开发中,常常需要对 PDF 文档进行各种操作,以下是一些常见的方法和技术要点,文章通过代码示例讲解的非常详细,需要的朋友可以参考下
    2025-08-08
  • C#采用FileSystemWatcher实现监视磁盘文件变更的方法

    C#采用FileSystemWatcher实现监视磁盘文件变更的方法

    这篇文章主要介绍了C#采用FileSystemWatcher实现监视磁盘文件变更的方法,详细分析了FileSystemWatcher的用法,并以此为基础实现监视磁盘文件变更,是非常实用的技巧,具有一定的借鉴价值,需要的朋友可以参考下
    2014-11-11
  • 使用MSScriptControl 在 C# 中读取json数据的方法

    使用MSScriptControl 在 C# 中读取json数据的方法

    下面小编就为大家带来一篇使用MSScriptControl 在 C# 中读取json数据的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • 使用C#代码在PDF文档添加页码的操作指南

    使用C#代码在PDF文档添加页码的操作指南

    向 PDF 文档添加页码不仅实用,而且具有美观效果,因为它能使文档呈现出类似专业出版物的精致外观,在本文中,您将学习如何在 C# 中使用 Spire.PDF for .NET 向 PDF 文档添加页码,需要的朋友可以参考下
    2025-11-11

最新评论