c# 第14节 字符方法、转义字符、字符串的方法

本节内容:

1:字符的定义

2:字符的方法

3: 转义字符

4:字符串简介

5:字符串方法

1:字符的定义

char与Unicode一一对应,一个char 2个字节。

2:字符的使用方法:

 实例:

 static void Main()
        {
            char a = 'a';
            char b = '0';
            char c = 'A';
            if (char.IsLetter(a)){ //判断小写
                Console.WriteLine("a是{0}小写,转大写是{1}", a, char.ToUpper(a));
            }
            if (char.IsDigit(b)){  //判断是否是数据
                Console.WriteLine("b是{0}数字;",b);            
            } 
            Console.WriteLine(char.ToLower(c)); //转换成小写
            Console.ReadKey();
        }

3:转义字符

 

4:字符串简介

 

字符集和字符编码的关系:

常用的字符集和字符编码:

ASCII码表的查看:

5:字符串的方法

1:将字符串转换成大小

 static void Main()
        {
            string res = "jack";
            char s1 = 's';
            Console.WriteLine(res.ToUpper()); //字符串变大写 
            Console.WriteLine(char.ToUpper(s1)); //字符变大写
            Console.ReadKey();
        }

2:字符串的比较

方法:4种

1: == 

2:String.Equals(arg1,arg2)  ##返回布尔值

3:String.Compare(arg1,arg2,[true|false])  ##可选的是true Or false 代表了是否可以忽略字符的大小写

4:compareto   ##arg1.compareto(arg2) 

  小于0 字符串1小于字符串2 
  等于0 字符串1等于字符串2 
  大于0 字符串1大于字符串2,或者字符串是null引用
namespace HelloWorld {
    class Hello
    {
        static void Main()
        {
            string res = "a"; //ascii 97
            string res2 = "b"; //ascii 98
            Console.WriteLine(string.Equals(res,res2));
            Console.WriteLine(string.Compare(res,res2));
            Console.WriteLine(res.CompareTo(res2));
    
            Console.ReadKey();
        }

    }
}

//false
// -1
//-1

 3:字符串格式化

format:格式
1:string.Format(要转换的格式,格式化对象);
2:string.Format(要转换的格式,格式化对象一,格式化对象二);

for (int i = 0; i < 50;i++ )
{
string mystr = string.Format("{0:D3}", i);
Console.WriteLine(mystr);

}

4:字符串的截取 (截取后的字符串是一个新的字符串)

str.Substring(number)  ##j从字符串的哪位开始截取

str.Substring(na,[nb]) ##从字符串的na到nb的截取

原理:根据字符串的索引进行截取 

namespace HelloWorld {
    class Hello
    {
        static void Main()
        {
            string str = "hello world";
            Console.WriteLine(str.Substring(2));
            Console.WriteLine(str.Substring(2,4));
    
            Console.ReadKey();
        }

    }
}

//llo world
//llo:

5:字符串的分割

用字符去分割: 

 static void Main()
        {
            string str = "hello world";
            string [] change_str = str.Split('o');
            foreach (string item in change_str) { 
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

用数组去分割:可以根据多个字符去分割

static void Main()
        {
            string str = "hello world";
            string [] change_str = str.Split(new char[]{'e','o'});
            foreach (string item in change_str) { 
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

输出:

6:字符串的合并

合并方法:
1: +
2: Concat string.Concat(字符串1,字符串2,字符串n...)
3: Join string.Join(合并后的分割符,字符串数组)

 static void Main()
        {
            string str = "hello";
            string str1 = "world";
            Console.WriteLine(str+str1);    //helloworld 
            Console.WriteLine(string.Concat(str,str1)); //helloworld 
            string [] str_array = {str,str1};
            Console.WriteLine(string.Join("|",str_array));//hello|world 
            Console.ReadKey();
        }

7:字符串的插入与填充 

插入:insert方法 

字符串.insert(插入位置,插入子串)

填充:

Padright、Padleft

Padright  方法是在尾部,添加其重复的字符,以达到总长度

字符串.Padright(总长度,【指定要填充的字符】) ##要是没有指定填充字符,默认为空格

static void Main()
        {
            string str = "hello";
            string insert_str = str.Insert(2, "***");
            Console.WriteLine(insert_str);  //he***llo
            string add_str = str.PadRight(10, '*');
            Console.WriteLine(add_str);
            string add_left_str = str.PadLeft(20, '#');
            Console.WriteLine(add_left_str);
            Console.ReadKey();
        }

输出:

8:字符串的删除

删除是通过字符串的Remove来实现的:
1:字符串.Remove(开始位置);
将字符串的开始位置后的所有字符都删除; 
2:字符串.Remove(开始位置,移出数m); 
从开始位置删除,删除【m】个数。

开始位置是指定字符串的索引,是一个整数,且小于字符串的长度。 

static void Main()
        {
            string str = "helloworld";
            string remove_str = str.Remove(2);
            Console.WriteLine(remove_str); //he
            string remove_str2 = str.Remove(2, 2); //heoworld
            Console.WriteLine(remove_str2);
            Console.ReadKey();
        }

9:字符串的修剪

 修剪即为Trim,用于删除字符串头尾出现的某些我们不想要的字符。 

方法:
字符串.Trim         ##默认删除字符串首部和尾部的空格
字符串.TrimStart()  ##默认只删除头部空格
字符串.TrimEnd()    ##默认只删除尾部空格

字符串.Trim(字符1,字符2) ##可以指定多个字符删除
    || 两个是相等的
字符串.Trim("包含所有需要修剪字符的字符串".ToCharArray())

实例:

 10:字符串的复制

复制是通过Copy和CopyTo来实现的。

1:string.Copy(要复制的字符串)
2:CopyTo(要复制字符的起始位置,目标字符数组,目标数组中的开始存放位置,要复制的字符个数)

namespace HelloWorld {
    class Hello
    {
        static void Main()
        {
            string str = "helloworldffff";
            string copy_str = string.Copy(str);
            Console.WriteLine(copy_str);
            char[] mychar = new char[20];
            str.CopyTo(2, mychar, 0, 3); //有选择的复制,从str的第二位置开始复制,复制3个数,存在mychar数组的0位置开始
            Console.WriteLine(mychar);                      
            Console.ReadKey();
        }

    }
}

//helloworldffff
//llo

11:字符串的替换

替换是通过Replace来实现的:
字符串.Replace(要替换的原字符串,替换后的字符串)

 static void Main()
        {
            string str = "crik";
            string replace_str = str.Replace("c","WW");
            Console.WriteLine(replace_str); //WWrik
            Console.ReadKey();
        }

12:字符串的查找 

查找是通过
IndexOf    ##从左往右 返回第一次找的字符索引的位置 没有找到返回-1
LastInderOf   ##从左往右 返回最后一次找的字符索引的位置 没有找到返回-1

  static void Main()
        {
            string str = "crikk";
            int indexof_str = str.IndexOf("k");
            Console.WriteLine(indexof_str);  //3
            int lastindexof_str = str.LastIndexOf('k');
            Console.WriteLine(lastindexof_str);  //4
            Console.ReadKey();
        }

13:类型的转换

类型转换有以下两种形式:

隐式转换:
    小往大转, 是系统自行的,不需要我们进行干预,也不需要 我们进行额外的编码。
显式转换:
    大往小转  会溢出:加上checked 检查  
    第一种显式:在要转换的变量前加上,(类型)
    第二种显式: Convert.ToInt32(要转换的类型)
 static void Main()
        {
            double a = 34.5;
            int b,c;
            b = (int)a;
            c = Convert.ToInt32(a); 
            Console.WriteLine(b); //34
            Console.WriteLine(c); //34 
            Console.ReadKey();
        }
原文地址:https://www.cnblogs.com/hero799/p/8656529.html