Home >Backend Development >C#.Net Tutorial >C# Learning Diary 15----Reference Type Summary of String Type Usage

C# Learning Diary 15----Reference Type Summary of String Type Usage

黄舟
黄舟Original
2017-01-20 13:55:351842browse

A basic class string is defined in C#, which is specially used to operate strings. This class is also defined in the namespace System of the .Net framework structure and is an alias of System.string. Strings are widely used, and many internal operations are encapsulated in the class definition. We only need to make simple use of them.

Create a string object:

string + object name (it’s very simple, I won’t go into details, focus on its usage)

Usage examples of string:

string str1 = "welcome";  
           string str2 = "WELCOME";  
           char c = str1[0];  // 返回指定字符  
  
           string str3 = str1 + str2;        //(+)表示连接两字符串 也可以用Contact(str1,str2)方法  
  
           bool b1 = (str1 == str2);       //判断是否相等  
  
           int i1 = string.Compare(str1, str2);    // 比较两字符串是否行等,相等返回0 不等如果str1>str2返回值大于0相反小于0  
  
           bool b2 = str3.Contains(str1);      //  str3中是否包含str1,返回true或false  
  
           string str4 = string.Copy(str1);    //复制  
  
           string str5 = string.Format("{0:f}", System.DateTime.Now);  //将系统时间格式化显示出来  
  
           int i2 = str2.IndexOf('E');      //返回字符 ‘E’在str2中第一个匹配的位置,找不到返回-1  
  
           int i3 = str2.LastIndexOf('E'); //返回 ‘E’在str2中最后一次出现的索引位置,索引从 0 开始。  
  
           string str6 = str1.Insert(1, "HC");   //  返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。  
  
           string str7 = str2.Remove(1, 3);   //从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。  
  
           string str8 = str2.Replace('E', 'e');  //从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。  
  
           string str9 = str2.Replace("COM", "com");  //所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。  
  
           string str10 = str2.ToLower();  //把字符串转换为小写并返回。  
  
           string str11 = str2.ToUpper();  //把字符串转换为大写并返回。  
  
           string str12 = str2.Trim();  //移除当前 String 对象中的所有前导空白字符和后置空白字符。  
  
           string str16 = string.Concat(str1, str2, str3, "HC666");  //连接4个字符串  
  
           bool str13 = str2.Equals(str1);  // 判断是否相等  
  
           bool str14 = str2.EndsWith("COME");   //判断 string 对象的结尾是否匹配指定的字符串。  
  
           bool str15 = str2.StartsWith("WEL");  //判断字符串实例的开头是否匹配指定的字符串。  
            
           string text = "welcome,to,HC666";  
           string[] str17 = text.Split((','));  // 将text字符串以(,)分配到数组 str17[0]="welcome" str17[1]="to" str17[2]="HC666"  
  
  
           string[] joi = {"welcome","to","HC666"};  
           string str18 = string.Join("-",joi);   //将 数组通过“-”连接成一个,结果 welcom-to-HC66

The above are the ones we often use, and there may be some more. That’s all I have learned for the time being. I won’t stick to the result graph for each function (too many, tired)

the above This is the content of C# Learning Diary 15----Summary of the usage of string type of reference type. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn