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#实现用于生成条形码的类

    C#实现用于生成条形码的类

    这篇文章主要介绍了C#实现用于生成条形码的类,涉及C#生成条形码的原理与实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • c# 网络编程之tcp

    c# 网络编程之tcp

    这篇文章主要介绍了c# 网络编程之tcp的的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • 浅谈C#.NET、JavaScript和JSON

    浅谈C#.NET、JavaScript和JSON

    本文介绍了C#.NET、JavaScript和JSON的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • C#实现的sqlserver操作类实例

    C#实现的sqlserver操作类实例

    这篇文章主要介绍了C#实现的sqlserver操作类,结合具体实例形式分析了C#针对sqlserver数据库进行连接、查询、更新、关闭等相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • C#实现微信退款及对账功能的示例详解

    C#实现微信退款及对账功能的示例详解

    在招聘报名系统里,考务费支付是其中一个环节,支付方式很多种,比如银联、微信、支付宝等等,本次我们以微信支付进行举例,在实际的应用中,对于支付成功的考生,我们会遇到实现退款的需求,所以本文给大家介绍了使用C#实现微信退款及对账,需要的朋友可以参考下
    2023-11-11
  • 使用C#来编写一个异步的Socket服务器

    使用C#来编写一个异步的Socket服务器

    这篇文章主要介绍了使用C#来编写一个异步的Socket服务器,通过无阻塞机制来获取更高的处理效率,需要的朋友可以参考下
    2015-07-07
  • C#编程自学之数据类型和变量二

    C#编程自学之数据类型和变量二

    这篇文章继续介绍了C#数据类型和变量,是对上一篇文章的补充,希望对大家的学习有所帮助。
    2015-10-10
  • C# 数独求解算法的实现

    C# 数独求解算法的实现

    这篇文章主要介绍了C# 数独求解算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 基于Unity3D实现3D照片墙效果

    基于Unity3D实现3D照片墙效果

    Unity3D不仅仅可以开发游戏,还有非常多的开发方向。本文就将用Unity3D制作出一个3D照片墙的效果,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-03-03
  • C#中添加窗口的步骤详解

    C#中添加窗口的步骤详解

    下面小编就为大家带来一篇C#中添加窗口的步骤详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02

最新评论