首页 > 编程语言 >C# 字符串(String)的使用

C# 字符串(String)的使用

时间:2023-03-07 12:57:40浏览次数:31  
标签:字符 String C# 指定 int 字符串 public string

C# 字符串(String)的使用

本文主要介绍C# 中字符串(String)的基础使用操作和相关方法使用(为变量分配字符串、字符串长度、字符串方法、、字符串连接、字符串格式化、转义字符),以及相关示例代码。

 

1、C# 字符串声明

字符串用于存文本内容。

C# 变量包含一组用双引号引起来的字符:

例如,

声明一个类型为string的变量,并为其分配一个值:

string cjavapy = "Hello";

2、C# 字符串长度

C# 中的string实际上是一个对象,其中包含可以对字符串执行某些操作的方法。例如,可以使用length()方法找到字符串的长度:

例如,

String txt = "www.cjavapy.com";
Console.WriteLine("txt字符串的长度: " + txt.Length);

3、字符串方法

有许多可用的字符串方法,例如,ToUpper()ToLower()

例如,

string txt = "Hello World";
Console.WriteLine(txt.ToUpper());   // 输出 "HELLO WORLD"
Console.WriteLine(txt.ToLower());   // 输出 "hello world"

4、查找字符在字符串中的索引位置

IndexOf()方法返回字符串(包括空格)中第一次出现的指定文本的index索引(位置):

例如,

string str1 = "abcdefghijklmnabc"; 
// 从头开始查找是否存在指定的字符 
Console.WriteLine(str1.IndexOf("c")); 
// 从第四个字符位置开始往后继续查找 
Console.WriteLine(str1.IndexOf("c", 3)); 
//若指定字符串中没有该字符则系统返回-1 
Console.WriteLine(str1.IndexOf("x")); 

C# 中位置下标从0开始计数。0是字符串中的第一个位置,1是第二个位置,2是第三个位置...等。

5、字符串连接

可以在字符串之间使用+运算符来拼接它们。

例如,

string a = "cjavapy";
string b = "com";
Console.WriteLine(a +"."+ b);

还可以使用Concat()方法来连接两个字符串:

例如,

string a = "cjavapy.";
string b = "com";
Console.WriteLine(string.Concat(a,b));

6、C# 特殊字符(转义字符)

因为字符串必须用引号引起来,所以C#会误解此字符串,并产生错误:

string txt = "the url is "cjavapy.com"";
Console.WriteLine(txt);

字符中使用的特殊字符是需要使用反斜杠转义才能包含在其中的。

反斜杠(\)转义字符将特殊字符转换为字符串字符:

 

转义符

结果

描述

\'

'

单引号

\"

"

双引号

\\

\

反斜杠

 

\“会在字符串中插入双引号:

例如,

string txt = "the url is \"cjavapy.com\"";
Console.WriteLine(txt);

\'会在字符串中插入一个单引号:

例如,

string txt = "the url\'s name is \"cjavapy.com\"";
Console.WriteLine(txt);

\\会在字符串中插入一个反斜杠:

例如,

string txt = "反斜杠:\\";
Console.WriteLine(txt);

C#中字符字符:

 

特殊字符

说明

\n

换行符

\r

回车符

\t

水平制表符(相当于tab,缩进)

\b

退格符(Backspace)

\f

换页符

 

7、字符串与数字相加

C# 使用+操作符可以进行数字加法运算和字符串连接。下面看一下如果字符串和数字使用+操作符的情况:

string x = "10";
int y = 20;
string z = x + y;   // z 的结果是 1020 (字符串)
Console.WriteLine(z);

8、字符串方法

 

序号

方法名称 & 描述

1

public static int Compare( string strA, string strB )

比较两个指定的 string 对象,

并返回一个表示它们在排列顺序中相对位置的整数。

该方法区分大小写。

2

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

比较两个指定的 string 对象,

并返回一个表示它们在排列顺序中相对位置的整数。

但是,如果布尔参数为真时,该方法不区分大小写。

3

public static string Concat( string str0, string str1 )

连接两个 string 对象。

4

public static string Concat( string str0, string str1, string str2 )

连接三个 string 对象。

5

public static string Concat( string str0, string str1, string str2, string str3 )

连接四个 string 对象。

6

public bool Contains( string value )

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

7

public static string Copy( string str )

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

8

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

从 string 对象的指定位置

开始复制指定数量的字符到 Unicode 字符数组中的指定位置。

9

public bool EndsWith( string value )

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

10

public bool Equals( string value )

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

11

public static bool Equals( string a, string b )

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

12

public static string Format( string format, Object arg0 )

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

13

public int IndexOf( char value )

返回指定 Unicode 字符在当前字符串中第一次出现的索引,

索引从 0 开始。

14

public int IndexOf( string value )

返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。

15

public int IndexOf( char value, int startIndex )

返回指定 Unicode 字符

从该字符串中指定字符位置开始搜索第一次出现的索引,

索引从 0 开始。

16

public int IndexOf( string value, int startIndex )

返回指定字符串

从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。

17

public int IndexOfAny( char[] anyOf )

返回某一个指定的 Unicode 字符数组中任意字符在该实例中第一次出现的索引,

索引从 0 开始。

18

public int IndexOfAny( char[] anyOf, int startIndex )

返回某一个指定的 Unicode 字符数组中任意字符,

从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。

19

public string Insert( int startIndex, string value )

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

20

public static bool IsNullOrEmpty( string value )

指示指定的字符串是否为 null 或者是否为一个空的字符串。

21

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

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

22

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

连接接一个字符串数组中的指定位置开始的指定元素,

使用指定的分隔符分隔每个元素。

23

public int LastIndexOf( char value )

返回指定 Unicode 字符在当前 string 对象中最后一次出现的索引位置,

索引从 0 开始。

24

public int LastIndexOf( string value )

返回指定字符串在当前 string 对象中最后一次出现的索引位置,

索引从 0 开始。

25

public string Remove( int startIndex )

移除当前实例中的所有字符,从指定位置开始,

一直到最后一个位置为止,并返回字符串。

26

public string Remove( int startIndex, int count )

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

27

public string Replace( char oldChar, char newChar )

把当前 string 对象中,

所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,

并返回新的字符串。

28

public string Replace( string oldValue, string newValue )

把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,

并返回新的字符串。

29

public string[] Split( params char[] separator )

返回一个字符串数组,包含当前的 string 对象中的子字符串,

子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。

30

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

返回一个字符串数组,包含当前的 string 对象中的子字符串,

子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。i

nt 参数指定要返回的子字符串的最大数目。

31

public bool StartsWith( string value )

判断字符串实例的开头是否匹配指定的字符串。

32

public char[] ToCharArray()

返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。

33

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

返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,

从指定的索引开始,直到指定的长度为止。

34

public string ToLower()

把字符串转换为小写并返回。

35

public string ToUpper()

把字符串转换为大写并返回。

36

public string Trim()

移除当前 String 对象中的所有前导空白字符和后置空白字符。

 

注意:上面字符串方法中,静态方法(static)需要使用String调用,例如,String.Concat("www",".","cjavapy",".","com"),非静态方法使用字符对象调用,例如,"www.cjavapy.com".IndexOf(".")  。

标签:字符,String,C#,指定,int,字符串,public,string
From: https://www.cnblogs.com/GaoUpUp/p/17187669.html

相关文章

  • 06、C++指针常量与常量指针
    constint*a;//指向常量int的指针int*consta;//指向int的常量指针,指针是常量voidf1(int*consta){//指针是常量std::cout<<*a<<std::endl;}voidf......
  • C# 布尔值(Boolean)
    C#布尔值(Boolean)C#中布尔值(Booleans)表示以下两个值之一:true或false。本文主要介绍布尔值(Boolean)的使用,bool与Boolean的区别,以及相关的示例代码。 1......
  • Use CMake notes
    WhayneedtheCMake?如果只是构建一个只有一个main.cpp的小型项目,那么确实不需要CMake,直接GCC、G++编译,或者写个build.sh脚本即可,不需要把简单的问题搞复杂化。$g++......
  • 07、clion中文乱码
    1、将全局编码和项目编码一级文件编码改为【UTF-8】2、按住快捷键:【Ctrl+Alt+Shift+/】,显示一下对话框3、取消勾选:【run.processes.with.pty】......
  • 08、VsCode配置gcc
    1.1下载gcchttps://github.com/niXman/mingw-builds-binaries/releases1.2配置环境变量bin目录略1.3vscode安装c/c++插件调试运行会生成c_cpp_properties.json和ta......
  • C# 获取用户输入和输出信息
    C#获取用户输入和输出信息本文主要介绍C#中使用Console类获取用户输入和输出信息的方法,以及相关示例代码。 1、Console类C# Console 类主要用于控制台......
  • C# 条件语句(If else)
    C#条件语句(Ifelse)C#if条件语句是通过一条或多条语句的执行结果(true或者false)来决定执行的代码块。本文主要介绍一下C#中条件语句(Ifelse)的使用。 ......
  • 02、C++字符串编码转换
    //string<——>wstring#include<codecvt>std::stringwstring2utf8string(conststd::wstring&str){staticstd::wstring_convert<std::codecvt_utf8<wchar_t>......
  • How can I access the UI element AutoSuggestEdit on wpf in model? I need reload t
    HowcanIaccesstheUIelementAutoSuggestEditonwpfinmodel?IneedreloadtheitemsourceofAutoSuggestEditwheninputtextchangedIntheMVVMpattern,......
  • 01、Clion+Qt+Opencv详细配置
    1、下载opencvgithub这里下载3.4版本的,下载4.0版本容易出错2、安装opencvD:\opencv\opencv-3.4.16\opencv3、CMake构建3.1添加源文件和目标文件3.2选择mingw构建......