C#基础知识学习(2)string类中的方法

1.Compare 比较字符串

用来比较2个字符串的长度大小和值是否相同,相同则返回0,当x比y小返回-1,否则返回1,如果长度相同,且值不同,则返回1,代码如下
public static void Main()
{
string x = "nihao";
string y = "nihao ma";结果:-1
//2.string x = "nihao ma";
//string y = "nihao";结果: 1
//3.string x = "nihao";
//string y = "nihao";结果: 0
//4.string x = "niliu";
//string y = "nihao";结果: 1
int result = string.Compare(x,y);
Console.WriteLine("结果:{0}",result);
Console.ReadKey();
}
2.String.CompareOrdinal 比较字符(还没有搞清楚,欢迎各位补充)
通过计算每个字符串中相应 System.Char 对象的数值来比较两个指定的 System.String 对象
public static int CompareOrdinal(string strA, string strB); 
 
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length); 
通过计算每个字符串中相应 System.Char 对象的数值来比较两个指定的 System.String 对象  
 
3.Concat 连接字符串
public static string Concat(paramsstring[] values);
用来连接多个字符串实例
string x="你好";
string y=",欢迎你";
string z=string.Concat(x,y);
结果是:你好,欢迎你
Concat和+的不同:
Concat只能用来连接字符串,同时是方法名。
+号是运算符,可以连接多种类型。
 
4.CopyTo
public void CopyTo(拷贝intsourceIndex,char[] destination,intdestinationIndex,intcount);
这个方法理解起来有点难度,只是找到下面的例子
string dest = "Hello world";
string source = "Goodbye China";
char[] destArray = dest.ToCharArray();//将dest变成字符数组
source.CopyTo(8, destArray, 6, 5);//从source的第8个字符起复制5个字符并从destArray的第6个位置开始放
dest = new string(destArray);//这时dest为"Hello China"
Console.WriteLine(dest);
 
  输出结果是:Hello China
 
5. IndexOf 索引字符的位置
定位字符
  • intIndexOf(charvalue)
  • intIndexOf(charvalue,intstartIndex)
  • intIndexOf(charvalue,intstartIndex,intcount)

定位字符串:

  1. intIndexOf(stringvalue)
  2. intIndexOf(stringvalue,intstartIndex)
  3. intIndexOf(stringvalue,intstartIndex,intcount)

在上述重载形式中,其参数含义如下:

Value:待定位的字符或者子串。

startIndex:在总串中开始搜索的起始位置。

Count:在总串中从起始位置开始搜索的字符数。

1)一个参数,索引字符第一次出现的位置,返回int 
例子:
String str1 = "hello world";
String str2 = "abcd";
int x = str1.IndexOf("o");
Console.WriteLine("结果是{0}",x);
结果是4,也就是说o在字符串str1中的位置是4.
indexof(string str,int i)
2)二个参数,表示从i+1的位置开始索引。
String str1 = "hello world";
String str2 = "abcd";
int x = str1.IndexOf("o");
int y = str1.IndexOf("o",5);
Console.WriteLine("结果是{0},定索引位置的索引结果是{1}",x,y);
Console.ReadKey();
结果是:7
定位从第5个开始搜索,搜索到o的位置是7,返回int 是7
3)三个参数,确定开始和要查的几位数的位置
indexof(string str,int i,int j)
String str1 = "hello world or happy you ";
int z = str1.IndexOf("o",10,4);
Console.WriteLine("倒序索引结果{0}",z);
从第10位开始查找,查询2位,结果是12
 
6.LastIndexOf 索引最后的位置
定位最后一次的位置,如果是三个参数,就是从后往前索引。
 
7.IndexOfAny
表示索引字符数组中,最近的一个值的位置。
String str1 = "hello world or happy you ";
char[] b = { 'e', 'o', 'l' };
int a = str1.IndexOfAny(b,5,15);
Console.WriteLine("结果是{0}}",a);
返回int为7
 
8.Insert 插入字符串
public string Insert(intstartIndex,stringvalue);
把一个字符串插入到另一个字符串的指定位置
String str1 = "hello world or happy you ";
String str2 = "abcd";
string str3 = str1.Insert(2, str2);
Console.WriteLine("insert功能插入结果{0}",str3);
这里是吧str2插入到str1中。插入位置2得到结果heabcdllo world or happy you
 
9.Join
在字符串数组中插入指定的字符
str5必须是字符串数组
 
string[] str5 = {"fsa","fasdf","fsaf"};
string str4 = string.Join("/", str5);
Console.WriteLine("Join功能结果{0}", str4);
输出结果fsa/fasdf/fsaf
 
 
10.PadLeft和PadRight
PadLeft是指在左面插入指定字符串总长度的char类型
string str1 = "hello world";
char str2='a';
string str3 = str1.PadLeft(12,str2);
Console.WriteLine("PadLeft功能插入结果{0}", str3);
结果是:ahello world
PadRight是指在右面插入指定字符串总长度的char类型
string str1 = "hello world";
char str2='a';
string str3 = str1.PadRight(12,str2);
Console.WriteLine("PadLeft功能插入结果{0}", str3);
结果是:hello worlda
 
11.Replace 替换字符串

 public string Replace(char oldChar, char newChar);

 public string Replace(string oldValue, string newValue);

string str1 = "hello world";
str1 = str1.Replace("d", "d!");
Console.WriteLine("Replace功能插入结果{0}", str1);
str1要更改的字符串,第一个参数是要更改的字符,第二个参数更新后的字符
结果是:hello world!
 
12.Split 分隔字符串
public string[] Split(paramschar[] separator);
string str1 = "hello world";
string[] str2 = str1.Split('w');
 结果是:hello world! 字符串数组,需要用for遍历获取,第一个参数是old,第二个参数是new
如果想获取分割后的单个值,可以这样使用
string str1 = "hello world";
string str2 = str1.Split('w')[0];
Console.WriteLine("分割数据{0}",str2);
这样就相当于获取数组中第一个值hello,如果是[1],那么获取的就是orld。
 
13.remove移除指定位置字符串
 public string Remove(int startIndex); 
移除的位置从sartIndex到结束位置的字符串
 public string Remove(int startIndex, int count); 
移除startIndex位置到count位置的字符串
string str1 = "changed";
string str4 = str1.Remove(1, 2);
str4得到的是cnged,移除了第二位和第三位。
 
14.Substring这个方法用的比较多(截取字符串)
String.Substring (Int32)         从此实例检索子字符串。子字符串从指定的字符位置开始。
string s = "Hello C# World!";
string s1=s.Substring(3);
Console.WriteLine(s1);
结果是:lo C# World!    //s1为从s中截取的位置为3的字符以后的字符子串,表示从第4位开始截取字符。
 
String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度
string s = "Hello C# World!";
string s1=s.Substring(3,2);
Console.WriteLine(s1);
结果是lo,后面一个参数限制了截取的位数。
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/myhomebo/p/5481307.html