zoukankan      html  css  js  c++  java
  • 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 方法按正常顺序(按字母顺序,如果是字符串的话)对列表中的所有项进行排序



  • 相关阅读:
    C# 读取sqlite文件
    MongoDB聚合管道
    提取Word里的文本内容 C#
    Two Sum【LeetCode】
    Could not create SSL/TLS secure channel.
    处理Task引发的异常
    https请求抛出异常
    hexo+github page +markdown问题汇总
    通过自定义比较器排序(C#版)
    GridView固定行宽,自动换行,鼠标放在Table的Tr上变色
  • 原文地址:https://www.cnblogs.com/zhangtaotqy/p/8671196.html
Copyright © 2011-2022 走看看