字符串string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _001_字符串string {
class Program {
static void Main(string[] args)
{
string s = "www.devsiki.com";//我们使用string类型去存储字符串类型 字符串需要使用双引号引起来
int length = s.Length;
//1、字符串相同与否判断
if (s == "www.devsiki.com")
{
Console.WriteLine("相同");
}
else
{
Console.WriteLine("不相同");
}
if (s.Equals("www.devsiki.com"))
{
Console.WriteLine("equals");
}
Console.WriteLine(length);
Console.WriteLine();
//2、字符串拼接
s = "http://" + s;
Console.WriteLine(s);
//3、获取字符串位置上的字符
char c = s[3];
Console.WriteLine(c);
for (int i = 0; i < s.Length; i++)
{
Console.Write(s[i]);
}
Console.WriteLine();
//4、字符串是否相等的对比
string s1 = "www.devsiki.com";//string 是System.String的别名
int res = s1.CompareTo("saki");//当两个字符串相等的时候,返回0 当s在字母表中的排序靠前的时候,返回-1, 否则返回1
Console.Write(res);
//5、把指定的字符换成指定的字符 或者把指定的字符串换成指定的字符串
string newStr = s1.Replace('.', '-');
Console.WriteLine(s1);
Console.WriteLine(newStr);
//6、分割字符串成为数组
string[] strArray = s1.Split('.');
foreach (var temp in strArray)
{
Console.WriteLine(temp);
}
//7、截取字符串
string str = s1.Substring(4);
Console.WriteLine(str);
//8、变为大写字母
string str1 = s1.ToUpper();
Console.Write(str1);
//9、去除空格
string str2 = s1.Trim();
Console.WriteLine(str2);
//10、我们可以使用这个方法判断当前字符串是否包含一个子字符串,如果不包含,返回-1,如果包含会返回第一个字符的索引
int index = s1.IndexOf("devsikid");
Console.WriteLine(index);
Console.ReadKey();
}
}
}