• C#之字符串


    1 Replace

    string sayHello = "Hello World!";
    Console.WriteLine(sayHello);
    sayHello = sayHello.Replace("Hello", "张涛");
    Console.WriteLine(sayHello);
    
    Hello World!
    张涛 World!
    Replace

    2.ToLower() 全部小写

    ToUpper()全部大写

    3.Contains

    string songLyrics = "You say goodbye, and I say hello";
    Console.WriteLine(songLyrics.Contains("goodbye"));
    Console.WriteLine(songLyrics.Contains("greetings"));
    True
    False
    
    string songLyrics = "You say goodbye, and I say hello";
    Console.WriteLine(songLyrics.StartsWith("You"));
    Console.WriteLine(songLyrics.StartsWith("goodbye"));
    
    Console.WriteLine(songLyrics.EndsWith("hello"));
    Console.WriteLine(songLyrics.EndsWith("goodbye"));
    
    True
    False
    True
    False
    Contains()

     4. IndexOf() 可能无法确定列表是否包含某项,因此,应始终检查 IndexOf 返回的索引。 如果索引为 -1,表明找不到相应项。

    var names = new List<string> { "<name>", "Ana", "Felipe" };
    names.Add("Maria");
    names.Add("Bill");
    names.Remove("Ana");
    
    var index = names.IndexOf("Felipe");
    if (index != -1)
      Console.WriteLine($"The name {names[index]} is at index {index}");
    
    var notFound = names.IndexOf("Not Found");
      Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");
    
    The name Felipe is at index 1
    When an item is not found, IndexOf returns -1
    IndexOf

    5. Sort() 还可以对列表中的项进行排序。 Sort 方法按正常顺序(按字母顺序,如果是字符串的话)对列表中的所有项进行排序



  • 相关阅读:
    etcd 部署、备份与恢复
    centos7 mysql 5.7.24 源码编译
    生产中两块网卡bond
    shell 免密批量执行脚本
    MegaCli 清除与添加raid5
    centos7 mongodb4.0.2 复制集主从部署
    centos6.6 部署 cacti 并采集交换机流量
    shell 批量远程主机执行命令
    拯救系统文件只读模式
    下推自动机(PDA)在程序设计中的应用
  • 原文地址:https://www.cnblogs.com/zhangtaotqy/p/8671196.html
Copyright © 2020-2023  润新知