亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# - 字符串

在 C# 中,您可以将字符串用作字符数组,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。 string 关键字是 System.String 的别名 类。

创建字符串对象

您可以使用以下方法之一创建字符串对象 -

以下示例演示了这一点 -

using System;

namespace StringApplication {

   class Program {
   
      static void Main(string[] args) {
         //from string literal and string concatenation
         string fname, lname;
         fname = "Rowan";
         lname = "Atkinson";
			
         char []letters= { 'H', 'e', 'l', 'l','o' };
         string [] sarray={ "Hello", "From", "Tutorials", "Point" };
         
         string fullname = fname + lname;
         Console.WriteLine("Full Name: {0}", fullname);
         
         //by using string constructor { 'H', 'e', 'l', 'l','o' };
         string greetings = new string(letters);
         Console.WriteLine("Greetings: {0}", greetings);
         
         //methods returning string { "Hello", "From", "Tutorials", "Point" };
         string message = String.Join(" ", sarray);
         Console.WriteLine("Message: {0}", message);
         
         //formatting method to convert a value
         DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
         string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
         Console.WriteLine("Message: {0}", chat);
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012

字符串类的属性

String 类具有以下两个属性 -

Sr.No. 属性和描述
1

字符

获取 Char 当前 String 中指定位置的对象 对象。

2

长度

获取当前 String 对象的字符数。

字符串类的方法

String 类有许多方法可以帮助您处理字符串对象。下表提供了一些最常用的方法 -


Sr.No. 方法和说明
1

public static int Compare(string strA, string strB)

比较两个指定的字符串对象并返回一个整数,表示它们在排序顺序中的相对位置。

2

public static int Compare(string strA, string strB, bool ignoreCase)

比较两个指定的字符串对象并返回一个整数,指示它们在排序顺序中的相对位置。但是,如果布尔参数为真,它会忽略大小写。

3

公共静态字符串 Concat(string str0, string str1)

连接两个字符串对象。

4

公共静态字符串 Concat(string str0, string str1, string str2)

连接三个字符串对象。

5

公共静态字符串 Concat(string str0, string str1, string str2, string str3)

连接四个字符串对象。

6

公共布尔包含(字符串值)

返回一个值,指示指定的 String 对象是否出现在此字符串中。

7

公共静态字符串复制(string str)

创建一个与指定字符串具有相同值的新 String 对象。

8

public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)

将指定数量的字符从 String 对象的指定位置复制到 Unicode 字符数组中的指定位置。

9

public bool EndsWith(字符串值)

判断字符串对象的结尾是否与指定的字符串匹配。

10

public bool Equals(字符串值)

判断当前的String对象和指定的String对象是否具有相同的值。

11

public static bool Equals(string a, string b)

判断两个指定的 String 对象是否具有相同的值。

12

公共静态字符串格式(字符串格式,对象arg0)

将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式。

13

public int IndexOf(char value)

返回当前字符串中指定 Unicode 字符第一次出现的从零开始的索引。

14

public int IndexOf(字符串值)

返回此实例中指定字符串第一次出现的从零开始的索引。

15

public int IndexOf(char value, int startIndex)

返回此字符串中指定 Unicode 字符第一次出现的从零开始的索引,从指定字符位置开始搜索。

16

public int IndexOf(string value, int startIndex)

返回此实例中指定字符串第一次出现的从零开始的索引,从指定字符位置开始搜索。

17

public int IndexOfAny(char[] anyOf)

返回指定 Unicode 字符数组中任何字符在此实例中第一次出现的从零开始的索引。

18

公共 int IndexOfAny(char[] anyOf, int startIndex)

返回指定 Unicode 字符数组中任何字符在此实例中第一次出现的从零开始的索引,从指定字符位置开始搜索。

19

公共字符串插入(int startIndex,字符串值)

返回一个新字符串,其中指定字符串插入到当前字符串对象的指定索引位置。

20

public static bool IsNullOrEmpty(字符串值)

指示指定的字符串是 null 还是 Empty 字符串。

21

public static string Join(string separator, params string[] value)

连接字符串数组的所有元素,在每个元素之间使用指定的分隔符。

22

public static string Join(string separator, string[] value, int startIndex, int count)

连接字符串数组的指定元素,在每个元素之间使用指定的分隔符。

23

public int LastIndexOf(char value)

返回当前字符串对象中指定 Unicode 字符最后一次出现的从零开始的索引位置。

24

public int LastIndexOf(字符串值)

返回当前字符串对象中指定字符串最后一次出现的从零开始的索引位置。

25

公共字符串删除(int startIndex)

删除当前实例中的所有字符,从指定位置开始一直到最后一个位置,并返回字符串。

26

公共字符串删除(int startIndex, int count)

从指定位置开始删除当前字符串中指定数量的字符并返回该字符串。

27

公共字符串替换(char oldChar, char newChar)

将当前字符串对象中出现的所有指定 Unicode 字符替换为指定的 Unicode 字符并返回新字符串。

28

公共字符串替换(字符串oldValue,字符串newValue)

将当前字符串对象中出现的所有指定字符串替换为指定字符串,并返回新字符串。

29

public string[] Split(params char[] 分隔符)

返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的 Unicode 字符数组的元素分隔。

30

public string[] Split(char[] separator, int count)

返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的 Unicode 字符数组的元素分隔。 int 参数指定要返回的最大子字符串数。

31

public bool StartsWith(字符串值)

确定此字符串实例的开头是否与指定的字符串匹配。

32

public char[] ToCharArray()

返回包含当前字符串对象中所有字符的 Unicode 字符数组。

33

public char[] ToCharArray(int startIndex, int length)

返回一个包含当前字符串对象中所有字符的 Unicode 字符数组,从指定的索引开始,一直到指定的长度。

34

公共字符串 ToLower()

返回此字符串转换为小写的副本。

35

公共字符串 ToUpper()

返回此字符串转换为大写的副本。

36

公共字符串 Trim()

从当前 String 对象中删除所有前导和尾随空白字符。


您可以访问 MSDN 库获取方法和 String 类构造函数的完整列表。

示例

下面的例子演示了上面提到的一些方法 -

比较字符串

using System;

namespace StringApplication {

   class StringProg {
   
      static void Main(string[] args) {
         string str1 = "This is test";
         string str2 = "This is text";

         if (String.Compare(str1, str2) == 0) {
            Console.WriteLine(str1 + " and " + str2 +  " are equal.");
         } else {
            Console.WriteLine(str1 + " and " + str2 + " are not equal.");
         }
         Console.ReadKey() ;
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

This is test and This is text are not equal.

字符串包含字符串

using System;

namespace StringApplication {

   class StringProg {
   
      static void Main(string[] args) {
         string str = "This is test";
         
         if (str.Contains("test")) {
            Console.WriteLine("The sequence 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

The sequence 'test' was found.

获取子字符串

using System;

namespace StringApplication {

   class StringProg {
   
      static void Main(string[] args) {
         string str = "Last night I dreamt of San Pedro";
         Console.WriteLine(str);
         string substr = str.Substring(23);
         Console.WriteLine(substr);
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

San Pedro

连接字符串

using System;

namespace StringApplication {

   class StringProg {
   
      static void Main(string[] args) {
         string[] starray = new string[]{"Down the way nights are dark",
            "And the sun shines daily on the mountain top",
            "I took a trip on a sailing ship",
            "And when I reached Jamaica",
            "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop

C语言

  1. C# 类和对象
  2. C# 字符串
  3. Java 字符串
  4. Java 单例类
  5. Java 枚举字符串
  6. C++ 字符串:strcpy()、strcat()、strlen()、strcmp() 示例
  7. C 中的字符串:如何声明变量、初始化、打印、示例
  8. Python 字符串:替换、连接、拆分、反转、大写和小写
  9. Java - 字符串类
  10. Java - 序列化
  11. Java 8 - 概述
  12. MATLAB - 字符串