Skip to content
成为赞助商

string常用方法 ​

1、Replace string替换字符串:

csharp
string st = "abcdef";

string newstring = st.Replace('a','x');

Console.WriteLine(newstring);	//即:xbcdef

****

csharp
string st = "abcdef";

string newstring = st.Remove(4);

Console.WriteLine(newstring);  //即:abcd

csharp
string st = "abcdef";

string newstring = st.Substring(2);

Console.WriteLine(newstring);  //即:cdef

public string Substring(int startIndex,int count); 从startIndex位置开始,提取count个字符。
如:  

string st = "abcdef";

string newstring = st.Substring(2,2);

Console.WriteLine(newstring);  //即:cd

csharp
string st = "语文|数学|英语|物理";

string[] split = st.Split(new char[]{'|'},2);

for (int i = 0; i < split.Length; i++)
{
    Console.WriteLine(split[i]);
}

** **

csharp
string str="";  
1、if(str=="")  
2、if(str==String.Empty)  
3、if(str.length==0) 
if(string.IsNullOrEmpty( str )) 
if(str!=null&&str.length==0)

csharp
string str="123456";  
str.Insert(0,"x");//x123456

最后更新于:

访客总数 人  总访问量 次  统计始于2024.10.29