c# ---字符串的操作

  1. 截取字符串

  • pulic string Substring(int startIndex, int Length)
  • startIndex : 子字符串中的起始位置的索引
  • Length      :子字符串中字符数

  • 在用Substring截取字符串时,如果length参数的长度大于截取字符串的长度,将从起始位置的索引出截取之后索引的字符.

新版本的VS会直接报出异常

  1. 分割字符串

  • public string[] split(params char[] separator)

  1. 插入和填充字符串

  • 插入字符串:
  • public string Insert(int startIndex, string value)
  • startIndex : 用于指定所要插入的位置,  索引从0开始.
  • Value        : 指定所要插入的字符串

  • 填充字符串

  • String类提供了PadLeft/PadRight方法用于填充字符串,PadLeft在字符串左侧进行字符填充。而PadRight就是在字符串右侧进行字符填充。
  • public string PadLeft(int totalWidth,  char paddingChar)
  • public string PadRight(int totalWidth,  char paddingChar)
  • totalWidth : 指定填充后的字符串的长度
  • paddingChar : 指定所要填充的字符,  如果省略,则填充空格字符。

  1. 删除字符串

  • public String Remove(int startIndex)
  • public String Remove(int startIndex , int count)
  • startIndex : 用于指定开始删除的位置, 索引从0开始
  • count        : 指定删除的字符数量
  • *@*  参数count的值不能为0或是负数(startIndex参数耶不能为负数) , 如果为负数将会引发ArgumentOutOfRangeException异常;如果为0,则删除无意义,也就是没有进行删除

 

  1. 复制字符串

  • Copy方法
  • public static string Copy(string str)
  • str : 要复制的字符串
  • 返回值 : 与str具有相同值的字符串.
  • CopyTo
  • CopyTo方法的功能与Copy类似, 但是CopyTo可以将字符串的某一部分复制到另一个数组中
  • public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
  • sourceIndex 需要复制的字符的起始位置
  • destination  目标字符数组
  • destinationIndex  指定目标数组中的开始存放位置
  • count  指定要复制的字符个数
  1. 替换字符串

  • public string Replace(char OChar,char NChar)
  • public string Replace(string OValue,string NValue)s
  • OChar   代替换的字符
  • NChar   替换后的新字符
  • OValue  代替换的新字符串
  • NValue  替换后的新字符串

猜你喜欢

转载自blog.csdn.net/qq_39059405/article/details/85371844
今日推荐