zoukankan      html  css  js  c++  java
  • 字符串的各种方法

    stringbuilder举例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace _05StringBuilder
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             StringBuilder sb = new StringBuilder();
    15             string str = "0";
    16             //创建了一个计时器,用来记录程序运行的时间
    17             //00:00:00.0422166
    18             Stopwatch sw = new Stopwatch();
    19             sw.Start();//开始计时
    20             for (int i = 0; i < 100000; i++)
    21             {
    22                 //str += i;
    23                 sb.Append(i);
    24             }
    25             sw.Stop();//结束计时
    26             Console.WriteLine(sb.ToString());
    27             Console.WriteLine(sw.Elapsed);
    28             Console.ReadKey();
    29         }
    30     }
    31 }
    View Code

    length举例:随机输入你心中想到的一个名字,然后输出它的字符串长度  length:可以得字符串的长度

    1 Console.WriteLine("请输入你心中想的那个人的名字");
    2 string name = Console.ReadLine();
    3 Console.WriteLine("你心中想的人的名字的长度是{0}", name.Length);
    4 Console.ReadKey();
    View Code

    toupper,tolower,equals举例:

     1 Console.WriteLine("请输入你喜欢的课程");
     2             string lessonOne = Console.ReadLine();
     3             //将字符串转换成大写
     4             //  lessonOne = lessonOne.ToUpper();
     5             //将字符串转换成小写形式
     6             // lessonOne = lessonOne.ToLower();
     7             Console.WriteLine("请输入你喜欢的课程");
     8             string lessonTwo = Console.ReadLine();
     9             //   lessonTwo = lessonTwo.ToUpper();
    10             //   lessonTwo = lessonTwo.ToLower();
    11             if (lessonOne.Equals(lessonTwo, StringComparison.OrdinalIgnoreCase))
    12             {
    13                 Console.WriteLine("你们俩喜欢的课程相同");
    14             }
    15             else
    16             {
    17                 Console.WriteLine("你们俩喜欢的课程不同");
    18             }
    19             Console.ReadKey();
    View Code

     split举例:从日期字符串("2008-08-08")中分析出年、月、日;2008年08月08日。

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

    contains,replace举例:

    1 string str = "国家关键人物老赵";
    2             if (str.Contains("老赵"))
    3             {
    4                 str = str.Replace("老赵", "**");
    5             }
    6             Console.WriteLine(str);
    7             Console.ReadKey();
    View Code

    substring举例:

    1 string str = "今天天气好晴朗,处处好风光";
    2             str = str.Substring(1, 2);
    3             Console.WriteLine(str);
    4             Console.ReadKey();
    View Code

    endswith举例:

     1  string str = "今天天气好晴朗,处处好风光";
     2             if (str.EndsWith(""))
     3             {
     4                 Console.WriteLine("是的");
     5             }
     6             else
     7             {
     8                 Console.WriteLine("不是的");
     9             }
    10             Console.ReadKey();
    View Code

    indexof,lastindexof举例:

     1 string str = "今天天天气好晴朗,天天处天好风光";
     2             int index = str.IndexOf('', 2);
     3             Console.WriteLine(index);
     4             Console.ReadKey();
     5 
     6 
     7             //string str = "今天天气好晴朗,处处好风光";
     8             //int index = str.LastIndexOf('天');
     9             //Console.WriteLine(index);
    10             //Console.ReadKey();
    11 
    12 
    13             ////LastIndexOf  Substring
    14             //string path = @"c:ac苍de苍fg\fdfdfdfdvfd苍老师苍.wav";
    15             //int index = path.LastIndexOf("\");
    16             //path = path.Substring(index + 1);
    17             //Console.WriteLine(path);
    18             //Console.ReadKey();
    View Code

    trim,trimstart,trimend举例:

    1  string str = "            hahahah          ";
    2             str = str.Trim();
    3             //str = str.TrimStart();
    4             //str = str.TrimEnd();
    5             Console.Write(str);
    6             Console.ReadKey();
    View Code

    string.IsNullOrEmpty,string.Join举例:

     1 string str = "fdsfdsfds";
     2             if (string.IsNullOrEmpty(str))
     3             {
     4                 Console.WriteLine("是的");
     5             }
     6             else
     7             {
     8                 Console.WriteLine("不是");
     9             }
    10             string[] names = { "张三", "李四", "王五", "赵六", "田七" };
    11             //张三|李四|王五|赵六|田七
    12             string strNew = string.Join("|", "张三", "李四", "王五", "赵六", "田七");
    13             Console.WriteLine(strNew);
    14             Console.ReadKey();
    View Code
  • 相关阅读:
    Zero-Copy&sendfile浅析
    Redis数据清除问题
    Redis官方文档》持久化
    GNU C 、ANSI C、标准C、标准c++区别和联系
    有关jQuery valid 验证多个同name属性的input的问题
    jQuery validator 增加多个模板
    archiver error. Connect internal only, until freed. 之解决办法
    链接ftp,把文件或图片上传到ftp指定的文件夹中
    java 数字金额转换中文金额
    java工具类 --千分位方法
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4452717.html
Copyright © 2011-2022 走看看