• c# Char && string


    char 支持的方法

    字符串
    声明字符串
    String str = [null];  可以用此方法声明一个空字符串
    

     

     
    连接字符串
    str +"" + str1;
    

      

    比较两个字符串
    Compare 静态方法 返回int
    比较两个字符串是否相等,最常用的2个重载方法
    Int Compare(string a,string b)
    Int Compare(string a,string b ,bool ignorCase) 第三方参数是true 忽略大小写
    String.Compare("aaa","bbb");
    

      

     
    CompareTo 返回int
      和compare差不多 不过他可以以实例对象本身与字符串对比
    int flag = str1.CompareTo("aab");
    

      

     
    Equals 比较两个字符串 成功返回 true 失败返回 false
    string a = "aab";
    string b = "aac";
    bool c = a.Equals(b);
    Console.WriteLine(c);
    

      

     
    Format 格式化字符串
    string newstr = String.Format("aaa{0} bbb {1}","1","2");
    Console.WriteLine(newstr);
    

      

     
    Substring(start,end) 截取字符串
    String a = "abcdefg";
    String b = ""; //我测试的 b字符串必须预先定义
    b = a.Substring(1,4);
    

      

     
    split 分割字符串成数组
    String a = "eeeabcdebcde";
    char[] sep = {'a','c'};
    String[] b = a.Split(sep);
    for (int i = 0; i < sep.Length; i++) {
      Console.WriteLine("item {0} {1}",i,b[i]);
    }
    

      

     
    Insert 字符串中插入
    String a = "abcdefg";
    string b;
    b = a.Insert(3,"hahaha");
    

      

     
     
    PadLeft PadRight 字符串填充
      Public String PadRight(int totalWidth,char c);
        totalWidth 填充后字符串的长度
        c 要填充的 char 类型字符
    String a = "abcdefg";
    string b;
    a = a.PadRight(9,'z');
    

      

     
    Remove 删除指定位置字符串
    String a = "abcdefg";
    a = a.Remove(3,2); //开始位置 从开始位置删除几个
    

      

     
    Copy 复制字符串
    String a = "abc";
    String b;
    b = String.Copy(a);
    

      

     
    CopyTo 和 copy差不多 不过可以复制字符串一部分到另一个数组里
    Public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
                  sourceIndex    需要复制的字符串起始位置
                    destination   目标字符数组
                    destinationIndex   制定目标数组中的开始存放位置
                    count  指定要复制的字符个数
    例子:
     String st1 = "用一生下载你";
                	char[] str = new char[100];
                	st1.CopyTo(1,str,0,4); //将字符串st从索引1开始的4个字符串复制到字符数组str中
    异常:ArgumentOutOfRangeException
    sourceIndex、destinationIndex 或 count 为负
    count 大于从 startIndex 到此实例末尾的子字符串的长度
    count 大于从 destinationIndex 到 destination 末尾的子数组的长度
    

      

     
    Replace 替换字符串
    public string Replace(char OChar,char NChar); 主要用于替换字符串中的字符
    public string Replace(string Ovalue,string NValue); 主要用于替换字符串中的字符串
    ochar 待替换的字符
    nchar 替换后的新字符
    ovalue 待替换的字符串
    nvalue 替换后的新字符串
    String str1 = "用一生下载你";
    string b = str1.Replace('一','二'); 替换字符
    string c = str1.Replace("用一生","去你妈的"); 替换字符串
    

      

  • 相关阅读:
    HTML导航
    html5学习笔记
    crm使用soap启用和停用记录
    crmjs区分窗口是否是高速编辑(2)
    关于strace的一点东西
    Android studio第一次使用配置(三)gradle项目构建
    IntelliJ IDEA 问题总结之二(待补充) —— 快捷键、主题样式、导出jar、sqlite
    spark之map与flatMap差别
    leetcode:String to Integer (atoi)
    oracle索引的操作
  • 原文地址:https://www.cnblogs.com/gwyy/p/8027468.html
Copyright © 2020-2023  润新知