字符串--重新赋值、转换数组、提供的方法

文章目录

1)、字符串的不可变性
当你给一个字符串重新赋值之后,老值并没有销毁,而是重新开辟一块空间存储新值。
当程序结束后,GC扫描整个内存,如果发现有的空间没有被指向,则立即把它销毁。

2)、我们可以讲字符串看做是char类型的一个只读数组。
ToCharArray();将字符串转换为char数组
new string(char[] chs):能够将char数组转换为字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04字符串
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //字符串的不可变性
            //string name = "张三";
            //name = "孙全";
            //Console.WriteLine(name);
            //Console.ReadKey();

            //string s1 = "张三";
            //string s2 = "张三";
            //Console.ReadKey();

            //可以讲string类型 看做是char类型的一个只读数组
            string s = "abcdefg";
            s = "bbcdefg";
            // s[0] = 'b';不能这样做  因为是只读的
            //首先将字符串转换为char类型的数组
            char[] chs = s.ToCharArray();
            chs[0] = 'b';
            //将字符数组转换为我们的字符串
            s = new string(chs);
            //既然可以将string看做char类型的只读数组,所以我可以通过下标去访问字符串中的某一个元素
            Console.WriteLine(s[0]);
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}

提供的方法:

1)、Length:获得当前字符串中字符的个数
2)、ToUpper():将字符转换成大写形式
3)、ToLower():将字符串转换成小写形式
4)、Equals(lessonTwo,StringComparison.OrdinalIgnoreCase):比较两个字符串,可以忽略大小写
5)、Split():分割字符串,返回字符串类型的数组。
6)、Substring():解决字符串。在截取的时候包含要截取的那个位置。
7)、IndexOf():判断某个字符串在字符串中第一次出现的位置,如果没有返回-1、值类型和引用类型在内存上存储的地方不一样。
8)、LastIndexOf():判断某个字符串在字符串中最后一次出现的位置,如果没有同样返回-1
9)、StartsWith():判断以....开始
10)、EndsWith():判断以...结束
11)、Replace():将字符串中某个字符串替换成一个新的字符串
12)、Contains():判断某个字符串是否包含指定的字符串
13)、Trim():去掉字符串中前后的空格
14)、TrimEnd():去掉字符串中结尾的空格
15)、TrimStart():去掉字符串中前面的空格
16)、string.IsNullOrEmpty():判断一个字符串是否为空或者为null
17)、string.Join():将数组按照指定的字符串连接,返回一个字符串。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06字符串的各种方法
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //练习一:随机输入你心中想到的一个名字,然后输出它的字符串长度  Length:可以得字符串的长度
            //Console.WriteLine("请输入心上人的名字: ");
            //string name = Console.ReadLine();
            //Console.WriteLine("你心中想的人的名字的长度是:     {0:0.00000000000000000}", name.Length);
            //Console.ReadKey();



            //Console.WriteLine("请输入你喜欢的课程");
            //string lessonOne = Console.ReadLine();
            将字符串转换成大写
            lessonOne = lessonOne.ToUpper();
            将字符串转换成小写形式
             lessonOne = lessonOne.ToLower();
            //Console.WriteLine("请输入你喜欢的课程");
            //string lessonTwo = Console.ReadLine();
               lessonTwo = lessonTwo.ToUpper();
               lessonTwo = lessonTwo.ToLower();
            //if (lessonOne.Equals(lessonTwo, StringComparison.OrdinalIgnoreCase))
            //{
    
    
            //    Console.WriteLine("你们俩喜欢的课程相同");
            //}
            //else
            //{
    
    
            //    Console.WriteLine("你们俩喜欢的课程不同");
            //}
            //Console.ReadKey();



            //string s = "a b   dfd _   +    =  ,,, fdf ";
            分割字符串split
            //char[] chs = { ' ', '_', '+', '=', ',' };
            //string[] str = s.split(chs, stringsplitoptions.removeemptyentries);
            //console.readkey();




            //练习:从日期字符串("2008-08-08")中分析出年、月、日;2008年08月08日。
            //让用户输入一个日期格式如:2008-01-02,你输出你输入的日期为2008年1月2日

            //string s = "2008-08-08";
              char[] chs = { '-' };
            //string[] date = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            //Console.WriteLine("{0}年{1}月{2}日", date[0], date[1], date[2]);
            //Console.ReadKey();
            //老赵

            //string str = "国家关键人物老赵";
            //if (str.Contains("老赵"))
            //{
    
    
            //    str = str.Replace("老赵", "**");
            //}
            //Console.WriteLine(str);
            //Console.ReadKey();



            //Substring 截取字符串

            //string str = "今天天气好晴朗,处处好风光";
            //str = str.Substring(1, 5);
            //Console.WriteLine(str);
            //Console.ReadKey();

            //string str = "今天天气好晴朗,处处好风";
            //if (str.EndsWith("风"))
            //{
    
    
            //    Console.WriteLine("是的");
            //}
            //else
            //{
    
    
            //    Console.WriteLine("不是的");
            //}
            //Console.ReadKey();



            //string str = "今天天天气好晴朗,天天处天好风光";
            //int index = str.IndexOf('哈',2);
            //Console.WriteLine(index);
            //Console.ReadKey();


            //string str = "今天天气好晴朗,处处好风光";
            //int index = str.LastIndexOf('天');
            //Console.WriteLine(index);
            //Console.ReadKey();


            LastIndexOf  Substring
            //string path = @"c:\a\b\c苍\d\e苍\f\g\\fd\fd\fdf\d\vfd\苍老师苍.wav";
            //int index = path.LastIndexOf("\\");
            //path = path.Substring(index+1);
            //Console.WriteLine(path);
            //Console.ReadKey();



            // string str = "            hahahah          ";
             str = str.Trim();
            // //str = str.TrimStart();
            // str = str.TrimEnd();
            // Console.Write(str);
            // Console.ReadKey();

            //string str = "fdsfdsfds";
            //if (string.IsNullOrEmpty(str))
            //{
    
    
            //    Console.WriteLine("是的");
            //}
            //else
            //{
    
    
            //    Console.WriteLine("不是");
            //}
            //string[] names = { "张三", "李四", "王五", "赵六", "田七" };
            张三|李四|王五|赵六|田七
            //string strNew = string.Join("|", "张三","李四","王五","赵六","田七");
            //Console.WriteLine(strNew);
            //Console.ReadKey();




        }
    }
}

猜你喜欢

转载自blog.csdn.net/dashuu/article/details/114450891
今日推荐