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



  • 相关阅读:
    程序的了解
    Oracle VM VirtualBox虚拟网卡消失解决方法
    YARN 运维、巡检、监控、调优、排障
    HDFS巡检、监控、调优、排障
    Windows CMD命令大全
    [HDU]6356 Glad You Came(ST表)
    [BZOJ] 1019 [SHOI2008]汉诺塔
    树上叶子之间点对距离平方和
    [BZOJ]1026[SCOI2009]windy数
    [计蒜客]A1542 The Maximum Unreachable Node Set
  • 原文地址:https://www.cnblogs.com/zhangtaotqy/p/8671196.html
Copyright © 2011-2022 走看看