using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study
{
class Program
{
static void Main(string[] args)
{
//test30(); //18.字符串常用处理方法
//test31(); //19.字符串截取
//test32(); //20.判断字符串是否相等
}
#region 18.字符串常用处理方法
static void test30()
{
string email = "123456@.qq.com";
int index = email.IndexOf("@");
int index1 = email.IndexOf("qq.com");
//索引从0开始计算
int index2 = email.IndexOf("qq.net");
//index=6
//index1=8
//index2= - 1 返回-1表示没有找到
Console.WriteLine($"{index}\t{index1}\t{index2}\t");
//获取长度
int length = email.Length; //获取字符串长度是通过length属性得到 属性首字符大写
Console.WriteLine("字符长度"+ length);
//字符串为中文时
string info = "你好呀";
Console.WriteLine(info .IndexOf("好")); //返回2
Console.WriteLine(info.Length); //返回3
}
#endregion
#region 19.字符串截取
static void test31()
{
string email = "123456@.qq.com";
string username = email.Substring(0,6);
Console.WriteLine(username); //返回123456 已知位置使用
//如果从某一个字符位置之前全部获取,那么只需要找到这个字符串的索引
string username1 = email.Substring(0, email.IndexOf("@"));//索引从0开始 未知位置使用,但知道需要截取字符
Console.WriteLine(username1);
string emailtype = email.Substring(email.IndexOf("@"));
Console.WriteLine(emailtype); //返回@.qq.com
//如果我们希望得到某一个字符串后面的全部内容,截取的位置是这个字符串索引位置+1
string emailtype1 = email.Substring(email.IndexOf("@")+1);//如果不需要@ (email.IndexOf("@")+1)
Console.WriteLine(emailtype1); //返回@.qq.com
}
#endregion
#region 20.判断字符串是否相等
static void test32()
{
string name = "amy";
string name1 = "amy";
string name2 = "amy1";
string name3 = "Amy2";
//字符串判断是否相等,在项目中实现用户登录账号或密码判断的时候经常使用。
Console.WriteLine(name == name1); //返回值true//判断是否相等用 == 不可以区分大小写。
Console.WriteLine(name1.Equals(name1)); //返回值true 可以区分大小写
Console.WriteLine(name1.Equals(name2)); //返回值false//Equals(平等。相同。) 等同于 == 可以区分大小写。
Console.WriteLine(name1.Equals(name3)); //返回值false //Equals(平等。相同。) 等同于 == 可以区分大小写。
//提醒 == & Equals 方法默认只能比较值类型或者字符串类型。对象类型或者引用类型不能使用这种方法比较
}
#endregion
}
}