C# 中的字符串

本章我们将更详细地处理字符串数据。 字符串在计算机语言中非常重要。 这就是为什么我们将整章专门用于 C# 中的字符串的原因。

C# 字符串定义

字符串是字符序列。 在 C# 中,字符串是 Unicode 字符序列。 它是一种数据类型,用于存储一系列数据值(通常为字节),其中元素通常根据字符编码代表字符。 当字符串从字面上出现在源代码中时,称为字符串文字。

字符串是对象。 有两个用于处理字符串的基本类:

  • System.String
  • System.Text.StringBuilder

String是不可变的字符序列。 StringBuilder是可变的字符序列。

在 C# 中,stringSystem.String的别名。 string是语言关键字,System.String是.NET 类型。

C# 初始化字符串

有多种创建字符串的方法,它们都是不可变的和可变的。 我们将展示其中的一些。

Program.cs

using System;
using System.Text;

namespace Initialization
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] cdb = { 'M', 'y', 'S', 'q', 'l' };

            string lang = "C#";
            String ide = "NetBeans";
            string db = new string(cdb);

            Console.WriteLine(lang);
            Console.WriteLine(ide);
            Console.WriteLine(db);

            StringBuilder sb1 = new StringBuilder(lang);
            StringBuilder sb2 = new StringBuilder();

            sb2.Append("Fields");
            sb2.Append(" of ");
            sb2.Append("glory");

            Console.WriteLine(sb1);
            Console.WriteLine(sb2);
        }
    }
}   

该示例显示了创建System.StringSystem.Text.StringBuilder对象的几种方法。

using System.Text;

该语句可以不加限制地使用System.Text.StringBuilder类型。

string lang = "C#";
String ide = "NetBeans";

最常见的方法是根据字符串文字创建字符串对象。

string db = new string(cdb);

在这里,我们从字符数组创建一个字符串对象。 stringSystem.String的别名。

StringBuilder sb1 = new StringBuilder(lang);

String创建一个StringBuilder对象。

StringBuilder sb2 = new StringBuilder();
sb2.Append("Fields");
sb2.Append(" of ");
sb2.Append("glory");

我们创建一个空的StringBuilder对象。 我们将三个字符串附加到对象中。

$ dotnet run
C#
NetBeans
MySql
C#
Fields of glory

运行示例可得出此结果。

C# 字符串插值

$特殊字符前缀将字符串文字标识为插值字符串。 插值字符串是可能包含插值表达式的字符串文字。

字符串格式化与字符串插值相似。 本章后面将介绍它。

Program.cs

using System;

namespace Interpolation
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 23;
            string name = "Peter";

            DateTime now = DateTime.Now;

            Console.WriteLine("{name} is {age} years old");
            Console.WriteLine("Hello, {name}! Today is {now.DayOfWeek}, 
                    it's {now:HH:mm} now");
        }
    }
}

该示例介绍了 C# 字符串插值。

Console.WriteLine($"{name} is {age} years old");

内插变量位于{}括号之间。

Console.WriteLine($"Hello, {name}! Today is {now.DayOfWeek}, 
    it's {now:HH:mm} now");

插值语法可以接收表达式或格式说明符。

$ dotnet run
Peter is 23 years old
Hello, Peter! Today is Friday, it's 14:58 now

这是输出。

C# 常规字符串

常规字符串可以包含解释的转义序列,例如换行符或制表符。 常规字符串放在一对双引号之间。

Program.cs

using System;
using System.Text;

namespace RegularLiterals
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "deep \t forest";
            string s2 = "deep \n forest";

            Console.WriteLine(s1);
            Console.WriteLine(s2);

            Console.WriteLine("C:\\Users\\Admin\\Documents");
        }
    }
}

该示例打印两个包含\t\n转义序列的字符串。

Console.WriteLine("C:\\Users\\Admin\\Documents");

使用时 路径,必须避开阴影。

$ dotnet run
deep     forest
deep
    forest
C:\Users\Admin\Documents    

C# 逐字字符串

逐字字符串不解释转义序列。 逐字字符串以@字符开头。 逐字字符串可用于多行字符串。

Program.cs

using System;

namespace VerbatimLiterals
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(@"deep \t forest");
            Console.WriteLine(@"C:\Users\Admin\Documents");

            var text = @"
            Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time.";

            Console.WriteLine(text);
        }
    }
}

在此代码示例中,我们使用逐字字符串。

Console.WriteLine(@"deep \t forest");

\t特殊字符不被解释; 它仅打印到控制台。

Console.WriteLine(@"C:\Users\Admin\Documents");

使用路径时,逐字字符串很方便; 不必逃避阴影。

var text = @"
    Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time.";

逐字字符串允许我们创建多行字符串。

$ dotnet run
deep \t forest
C:\Users\Admin\Documents

            Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time.

C# 字符串是对象

字符串是对象。 它们是参考类型。 字符串是System.StringSystem.Text.StringBuilder类的实例。 由于它们是对象,因此有多种方法可用于完成各种工作。

Program.cs

using System;

namespace Objects
{
    class Program
    {
        static void Main(string[] args)
        {
            string lang = "Java";

            string bclass = lang.GetType().Name;
            Console.WriteLine(bclass);

            string parclass = lang.GetType().BaseType.Name;
            Console.WriteLine(parclass);

            if (lang.Equals(String.Empty))
            {

                Console.WriteLine("The string is empty");
            }
            else
            {

                Console.WriteLine("The string is not empty");
            }

            int len = lang.Length;
            Console.WriteLine("The string has {0} characters", len);
        }
    }
}

在此程序中,我们演示了字符串是对象。 对象必须具有一个类名,一个父类,并且还必须具有一些我们可以调用的方法或要访问的属性。

string lang = "Java";

创建System.String类型的对象。

string bclass = lang.GetType().Name;
Console.WriteLine(bclass);

我们确定lang变量所引用的对象的类名称。

string parclass = lang.GetType().BaseType.Name;
Console.WriteLine(parclass);

接收到我们对象的父类。 所有对象都有至少一个父对象-Object

if (lang.Equals(String.Empty)) 
{    
    Console.WriteLine("The string is empty");
} else 
{    
    Console.WriteLine("The string is not empty");
}

对象具有各种方法。 使用Equals()方法,我们检查字符串是否为空。

int len = lang.Length;
Console.WriteLine("The string has {0} characters", len);

Length()方法返回字符串的大小。

$ dotnet run
String
Object
The string is not empty
The string has 4 characters

这是 stringobjects.exe 程序的输出。

C# 可变&不可变字符串

String是不可变字符序列,而StringBuilder是可变字符序列。 下一个示例将显示差异。

Program.cs

using System;
using System.Text;

namespace MutableImmutable
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Jane";
            string name2 = name.Replace('J', 'K');
            string name3 = name2.Replace('n', 't');

            Console.WriteLine(name);
            Console.WriteLine(name3);

            StringBuilder sb = new StringBuilder("Jane");
            Console.WriteLine(sb);

            sb.Replace('J', 'K', 0, 1);
            sb.Replace('n', 't', 2, 1);

            Console.WriteLine(sb);
        }
    }
}

这两个对象都有替换字符串中字符的方法。

string name = "Jane";
string name2 = name.Replace('J', 'K');
string name3 = name2.Replace('n', 't');

String上调用Replace()方法将导致返回新的修改后的字符串。 原始字符串不变。

sb.Replace('J', 'K', 0, 1);
sb.Replace('n', 't', 2, 1);

StringBuilderReplace()方法将用新字符替换给定索引处的字符。 原始字符串被修改。

$ dotnet run
Jane
Kate
Jane
Kate

这是程序的输出。

C# 连接字符串

可以使用+运算符或Concat()方法添加不可变的字符串。 它们将形成一个新字符串,该字符串是所有连接字符串的链。 可变字符串具有Append()方法,该方法可以从任意数量的其他字符串中构建一个字符串。

也可以使用字符串格式设置和插值来连接字符串。

Program.cs

using System;
using System.Text;

namespace Concatenate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Return" + " of " + "the king.");

            Console.WriteLine(string.Concat(string.Concat("Return", " of "),
                "the king."));

            StringBuilder sb = new StringBuilder();
            sb.Append("Return");
            sb.Append(" of ");
            sb.Append("the king.");

            Console.WriteLine(sb);

            string s1 = "Return";
            string s2 = "of";
            string s3 = "the king.";

            Console.WriteLine("{0} {1} {2}", s1, s2, s3);
            Console.WriteLine($"{s1} {s2} {s3}");
        }
    }
}

该示例通过连接字符串创建五个句子。

Console.WriteLine("Return" + " of " + "the king.");

通过使用+运算符形成一个新的字符串。

Console.WriteLine(string.Concat(string.Concat("Return", " of "), 
    "the king."));

Concat()方法连接两个字符串。 该方法是System.String类的静态方法。

StringBuilder sb = new StringBuilder();
sb.Append("Return");
sb.Append(" of ");
sb.Append("the king.");

通过三次调用Append()方法来创建StringBuilder类型的可变对象。

Console.WriteLine("{0} {1} {2}", s1, s2, s3);

字符串以字符串格式形成。

Console.WriteLine($"{s1} {s2} {s3}");

最后,使用插值语法添加字符串。

$ dotnet run
Return of the king.
Return of the king.
Return of the king.
Return of the king.
Return of the king.

这是示例输出。

C# 使用引号

当我们要显示引号时,例如在直接语音中,必须对内引号进行转义。

Program.cs

using System;

namespace Quotes
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("There are many stars.");
            Console.WriteLine("He said, \"Which one is your favourite?\"");

            Console.WriteLine(@"
            Lao Tzu has said: 
            ""If you do not change direction, you may end up 
            where you are heading.""
            ");
        }
    }
}

本示例打印直接语音。

Console.WriteLine("He said, \"Which one is your favourite?\"");

在常规字符串中,该字符使用\进行转义。

Console.WriteLine(@"
Lao Tzu has said: 
""If you do not change direction, you may end up 
where you are heading.""
");

在逐字字符串中,引号前面带有另一个引号。

$ dotnet run
There are many stars.
He said, "Which one is your favourite?"

            Lao Tzu has said: "If you do not change direction, you may end up
            where you are heading."

This is the output of the program.

C# 比较字符串

我们可以使用==运算符比较两个字符串。

Program.cs

using System;

namespace CompareString
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("12" == "12");
            Console.WriteLine("17" == "9");
            Console.WriteLine("aa" == "ab");
        }
    }
}

在示例程序中,我们比较字符串。

$ dotnet run
True
False
False

这是程序的输出。

string.Compare()方法比较两个指定的字符串,并返回一个整数,该整数指示排序顺序中它们的相对位置。 如果返回的值小于零,则第一个字符串小于第二个字符串。 如果返回零,则两个字符串相等。 最后,如果返回的值大于零,则第一个字符串大于第二个字符串。

Program.cs

using System;

namespace CompareString2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "ZetCode";
            string str2 = "zetcode";

            Console.WriteLine(string.Compare(str1, str2, true));
            Console.WriteLine(string.Compare(str1, str2, false));
        }
    }
}

有一个可选的第三个ignoreCase参数。 它确定是否应履行此案。

Console.WriteLine(string.Compare(str1, str2, true));

比较两个字符串并忽略大小写。 此行将 0 打印到控制台。

C# 字符串元素

字符串是字符序列。 字符是字符串的基本元素。

Program.cs

using System;

namespace StringElements
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] crs = { 'Z', 'e', 't', 'C', 'o', 'd', 'e' };
            String s = new String(crs);

            char c1 = s[0];
            char c2 = s[(s.Length - 1)];

            Console.WriteLine(c1);
            Console.WriteLine(c2);

            int i1 = s.IndexOf('e');
            int i2 = s.LastIndexOf('e');

            Console.WriteLine("The first index of character e is " + i1);
            Console.WriteLine("The last index of character e is " + i2);

            Console.WriteLine(s.Contains("t"));
            Console.WriteLine(s.Contains("f"));

            char[] elements = s.ToCharArray();

            foreach (char el in elements)
            {
                Console.WriteLine(el);
            }
        }
    }
}

在第一个示例中,我们将使用不可变的字符串。

char[] crs = {'Z', 'e', 't', 'C', 'o', 'd', 'e' };
String s = new String(crs);

由字符数组构成一个新的不可变字符串。

char c1 = s[0];
char c2 = s[(s.Length-1)];

使用数组访问符号,我们获得字符串的第一个和最后一个 char 值。

int i1 = s.IndexOf('e');
int i2 = s.LastIndexOf('e');

通过上述方法,我们得到了字符“ e”的第一个和最后一个出现。

Console.WriteLine(s.Contains("t"));
Console.WriteLine(s.Contains("f"));

使用Contains()方法,我们检查字符串是否包含’t’字符。 该方法返回一个布尔值。

char[] elements = s.ToCharArray();

foreach (char el in elements) 
{    
    Console.WriteLine(el);
} 

ToCharArray()方法从字符串创建一个字符数组。 我们遍历数组并打印每个字符。

$ dotnet run
Z
e
The first index of character e is 1
The last index of character e is 6
True
False
Z
e
t
C
o
d
e

在第二个示例中,我们将使用可变字符串的元素。

Program.cs

using System;
using System.Text;

public class StringBuilderElements
{
    static void Main() 
    {
        StringBuilder sb = new StringBuilder("Misty mountains");
        Console.WriteLine(sb);

        sb.Remove(sb.Length-1, 1);
        Console.WriteLine(sb);

        sb.Append('s');
        Console.WriteLine(sb);

        sb.Insert(0, 'T');
        sb.Insert(1, 'h');
        sb.Insert(2, 'e');
        sb.Insert(3, ' ');
        Console.WriteLine(sb);

        sb.Replace('M', 'm', 4, 1);
        Console.WriteLine(sb); 
    }
}

形成可变的字符串。 我们通过删除,附加,插入和替换字符来修改字符串的内容。

sb.Remove(sb.Length-1, 1);

这行删除最后一个字符。

sb.Append('s');

删除的字符将附加回字符串。

sb.Insert(0, 'T');
sb.Insert(1, 'h');
sb.Insert(2, 'e');
sb.Insert(3, ' ');

我们在字符串的开头插入四个字符。

sb.Replace('M', 'm', 4, 1);

最后,我们在索引 4 处替换一个字符。

$ dotnet run
Misty mountains
Misty mountain
Misty mountains
The Misty mountains
The misty mountains

从输出中,我们可以看到可变字符串是如何变化的。

C# 字符串连接和拆分

Join()连接字符串,Split()拆分字符串。

Program.cs

using System;

namespace JoinSplit
{
    class Program
    {
        static void Main(string[] args)
        {
            var items = new string[] { "C#", "Visual Basic", "Java", "Perl" };

            var langs = string.Join(",", items);
            Console.WriteLine(langs);

            string[] langs2 = langs.Split(',');

            foreach (string lang in langs2)
            {
                Console.WriteLine(lang);
            }
        }
    }
}  

在我们的程序中,我们将连接和分割字符串。

var items = new string[] { "C#", "Visual Basic", "Java", "Perl" };

这是一个字符串数组。 这些字符串将被连接。

string langs = string.Join(",", items);

数组中的所有单词都被加入。 我们从中构建一个字符串。 每两个字之间会有一个逗号。

string[] langs2 = langs.Split(',');

作为反向操作,我们分割了 langs 字符串。 Split()方法返回由字符分隔的单词数组。 在我们的情况下,它是一个逗号字符。

foreach (string lang in langs2)
{
    Console.WriteLine(lang);
}

我们遍历数组并打印其元素。

$ dotnet run
C#,Visual Basic,Java,Perl
C#
Visual Basic
Java
Perl

这是示例的输出。

C# 常用方法

接下来,我们介绍了几个其他的常见字符串方法。

Program.cs

using System;

namespace CommonMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            string word = "Determination";

            Console.WriteLine(word.Contains("e"));
            Console.WriteLine(word.IndexOf("e"));
            Console.WriteLine(word.LastIndexOf("i"));

            Console.WriteLine(word.ToUpper());
            Console.WriteLine(word.ToLower());
        }
    }
}

在上面的示例中,我们介绍了五个字符串方法。

Console.WriteLine(str.Contains("e"));

如果字符串包含特定字符,则Contains()方法返回True

Console.WriteLine(str.IndexOf("e"));

IndexOf()返回字符串中字母的第一个索引。

Console.WriteLine(str.LastIndexOf("i"));

LastIndexOf()方法返回字符串中字母的最后一个索引。

Console.WriteLine(str.ToUpper());
Console.WriteLine(str.ToLower());

字符串的字母通过ToUpper()方法转换为大写,并通过ToLower()方法转换为小写。

$ dotnet run
True
1
10
DETERMINATION
determination

运行程序。

C# 字符串复制与克隆

我们将描述两种方法之间的区别:Copy()Clone()Copy()方法创建一个新字符串实例,该实例的值与指定的字符串相同。 Clone()方法返回对正在克隆的字符串的引用。 它不是堆上字符串的独立副本。 它是同一字符串上的另一个引用。

Program.cs

using System;

namespace CopyClone
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "ZetCode";

            string cloned = (string) str.Clone();
            string copied = string.Copy(str);

            Console.WriteLine(str.Equals(cloned)); // prints True
            Console.WriteLine(str.Equals(copied)); // prints True

            Console.WriteLine(ReferenceEquals(str, cloned)); // prints True
            Console.WriteLine(ReferenceEquals(str, copied)); // prints False
        }
    }
}

我们的示例演示了两种方法之间的区别。

string cloned = (string) str.Clone();
string copied = string.Copy(str);

字符串值被克隆并复制。

Console.WriteLine(str.Equals(cloned)); // prints True
Console.WriteLine(str.Equals(copied)); // prints True

Equals()方法确定两个字符串对象是否具有相同的值。 所有三个字符串的内容都是相同的。

Console.WriteLine(ReferenceEquals(str, cloned)); // prints True
Console.WriteLine(ReferenceEquals(str, copied)); // prints False

ReferenceEquals()方法比较两个参考对象。 因此,将复制的字符串与原始字符串进行比较将返回 false。 因为它们是两个不同的对象。

C# 格式化字符串

在下面的示例中,我们将格式化字符串。 .NET Framework 具有称为复合格式的功能。 Format()WriteLine()方法支持它。 方法采用对象列表和复合格式字符串作为输入。 格式字符串由固定字符串和一些格式项组成。 这些格式项是与列表中的对象相对应的索引占位符。

格式项具有以下语法:

{index[,length][:formatString]}

索引组件是必需的。 它是一个从 0 开始的数字,表示对象列表中的一项。 多个项目可以引用对象列表的同一元素。 如果格式项未引用该对象,则将其忽略。 如果我们在对象列表的范围之外引用,则会抛出运行时异常。

长度部分是可选的。 它是参数的字符串表示形式中的最小字符数。 如果为正,则该参数为右对齐;否则为 0。 如果为负,则为左对齐。 如果指定,则必须用冒号分隔索引和长度。

formatString 是可选的。 它是一个格式化值的字符串,是一种特定的方式。 它可以用来格式化日期,时间,数字或枚举。

在这里,我们展示了如何使用格式项的长度分量。 我们将三列数字打印到终端。 左,中和右对齐。

Program.cs

using System;

namespace Format1
{
    class Program
    {
        static void Main(string[] args)
        {
            int oranges = 2;
            int apples = 4;
            int bananas = 3;

            string str1 = "There are {0} oranges, {1} apples and {2} bananas";
            string str2 = "There are {1} oranges, {2} bananas and {0} apples";

            Console.WriteLine(str1, oranges, apples, bananas);
            Console.WriteLine(str2, apples, oranges, bananas);
        }
    }
}   

我们向控制台打印一条简单的消息。 我们仅使用格式项的索引部分。

string str1 = "There are {0} oranges, {1} apples and {2} bananas";

{0}{1}{2}是格式项。 我们指定索引组件。 其他组件是可选的。

Console.WriteLine(str1, oranges, apples, bananas);

现在,我们将复合格式放在一起。 我们有字符串和对象列表(橙色,苹果,香蕉)。 {0}格式项目是指橙色。 WriteLine()方法将{0}格式项替换为 oranges 变量的内容。

string str2 = "There are {1} oranges, {2} bananas and {0} apples";

引用对象的格式项的顺序很重要。

$ dotnet run
There are 2 oranges, 4 apples and 3 bananas
There are 2 oranges, 3 bananas and 4 apples

我们可以看到该程序的结果。

下一个示例将格式化数字数据。

Program.cs

using System;

namespace Format2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0}  {1, 12}", "Decimal", "Hexadecimal");

            Console.WriteLine("{0:D}  {1,8:X}", 502, 546);
            Console.WriteLine("{0:D}  {1,8:X}", 345, 765);
            Console.WriteLine("{0:D}  {1,8:X}", 320, 654);
            Console.WriteLine("{0:D}  {1,8:X}", 120, 834);
            Console.WriteLine("{0:D}  {1,8:X}", 620, 454);
        }
    }
}

我们以十进制和十六进制格式打印数字。 我们还使用长度分量对齐数字。

Console.WriteLine("{0:D}  {1,8:X}", 502, 546);;

{0:D}格式项指定,将采用提供的对象列表中的第一项并将其格式化为十进制格式。 {1,8:X}格式项目取第二项。 将其格式化为十六进制格式:X。 字符串长度为 8 个字符8 。 因为数字只有三个字符,所以它会右对齐并用空字符串填充。

$ dotnet run
Decimal   Hexadecimal
502       222
345       2FD
320       28E
120       342
620       1C6

运行示例,我们得到了这个结果。

最后两个示例将格式化数字和日期数据。

Program.cs

using System;

namespace Format3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Format("Number: {0:N}", 126));
            Console.WriteLine(string.Format("Scientific: {0:E}", 126));
            Console.WriteLine(string.Format("Currency: {0:C}", 126));
            Console.WriteLine(string.Format("Percent: {0:P}", 126));
            Console.WriteLine(string.Format("Hexadecimal: {0:X}", 126));
        }
    }
}

该示例演示了数字的标准格式说明符。 数字 126 以五种不同格式打印:普通,科学,货币,百分比和十六进制。

$ dotnet run
Number: 126.00
Scientific: 1.260000E+002
Currency: $126.00
Percent: 12,600.00%
Hexadecimal: 7E

最后,我们将格式化日期和时间数据。

Program.cs

using System;

namespace Format4
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime today = DateTime.Now;

            Console.WriteLine(string.Format("Short date: {0:d}", today));
            Console.WriteLine(string.Format("Long date: {0:D}", today));
            Console.WriteLine(string.Format("Short time: {0:t}", today));
            Console.WriteLine(string.Format("Long time: {0:T}", today));
            Console.WriteLine(string.Format("Month: {0:M}", today));
            Console.WriteLine(string.Format("Year: {0:Y}", today));
        }
    }
}

该代码示例显示了当前日期和时间的六种不同格式。

$ dotnet run
Short date: 10/24/2019
Long date: Thursday, October 24, 2019
Short time: 1:37 PM
Long time: 1:37:38 PM
Month: October 24
Year: October 2019

这是示例的输出。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程