当前位置:主页 > 软件编程 > .NET代码 >

C# 字符串、数组和List的截取和转换实例

时间:2020-11-17 01:35:40 | 栏目:.NET代码 | 点击:

如下所示:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace ConsoleApp1
{
  class Program
  {
    /// <summary>
    /// 字符串,数组和List的截取,转换
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
      string str = "abcdefghij1234567890";
      int i = 4;
      string str1 = str.Substring(0, i);//截取字符串前i位--abcd
      string str2 = str.Remove(i, str.Length - i);//移除字符串i位后面的字符=截取字符串前i位--abcd
      string str3 = str.Remove(0, i);//截取字符串i位之后的字符串--efghij1234567890
      string str4 = str.Substring(i);//截取字符串i位之后的字符串--efghij1234567890
      string str5 = str.Substring(str.Length - i);//截取字符串后i位--7890
      string str6 = str.Remove(0, str.Length - i);//截取字符串后i位--7890
      string str7 = str.Substring(0, str.Length - i);//去掉字符串后i位--abcdefghij123456
      string str8 = str.Remove(str.Length - i, i);//去掉字符串后i位--abcdefghij123456
      string str9 = str.Replace("abc", "ABC");//替换字符串中的字符串--ABCdefghij1234567890
      string str0 = str.ToUpper();//小写字母转换成大写字母--ABCDEFGHIJ1234567890
      string str10 = str0.ToLower();//大写字母转换成小写字母--abcdefghij1234567890
      string str11= str.Substring(str.Length - 1, 1);//截取字符串最后一位--0
      int m = str.IndexOf("cde") + 1;
      int n = str.IndexOf("23");
      string str12 = str.Substring(m, n - m + 2);//截取从开始字符串到结束字符串范围--cdefghij123
      string s = "a,b,c,d,e,f,g,h,i,j";
      string[] strArray = s.Split(','); //字符串转数组
      string str13 = string.Join(",", strArray);//数组转字符串
      List<string> list = new List<string>(s.Split(','));//字符串转List
      string str14 = string.Join(",", list.ToArray());//List转字符串
      string[] str15 = list.ToArray();//List转数组
      List<string> listS = new List<string>(str15);//数组转List
      Console.WriteLine(str0);
      Console.WriteLine(str12);
      Console.ReadLine();
    }
  }
   
}

您可能感兴趣的文章:

相关文章