开发者

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

目录
  • ToUpper()/ToLower()
  • Equals()
  • Split()
  • Substring()
  • IndexOf()/LastIndexOf()
  • StartsWith()/EndsWith()
  • Replace()
  • Contains()
  • Trim()/TrimEnd()/TrimStart()
  • IsNullOrEmpty()
  • 总结 

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

作用:从某一下标开始,截取字符串,返回截取后的javascript字符串。

使用:被截取的字符串.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();
        }
http://www.devze.com    }
}

第一次查找,找到了"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

Fapythonlse

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#字符串常用方法内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜