comeonbabe_
2017-09-14 00:11
采纳率: 63.6%
浏览 3.6k

C#字符数组的某一个怎么替换和删除

就是比如说把第几个字符到第几个字符之间的这些替换成其他字符或者null

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • XianRenShan 2017-09-14 00:35
    已采纳

    string s="hjshfkjhkhaE12461564";
    int i=s.IndexOf("E");
    string str=s.Substring(i+1,3);
    s=s.Replace(str,"替换成你的字符串"); //若为"",就是替换为空
    MessageBox.Show(s);

    打赏 评论
  • threenewbee 2017-09-14 00:29

    string str = "1234567";
    str = Regex.Replace(str, "(?<=.{几个字符}\w+?)", 替换成);

    打赏 评论
  • threenewbee 2017-09-14 00:29

    string str = "1234567";
    str = Regex.Replace(str, @"(?<=.{几个字符}\w+?)", 替换成);

    打赏 评论
  • jiminy_cn 2017-09-14 01:14

    是字符串吗?还是字符数组?看看下面代码,请采纳。

            int startIndex = 5;
            int endIndex = 8;
            string newString = "abcdef";
            string originalString = @"0123456789";
    
    
            string resultString = originalString.Remove(startIndex, endIndex - startIndex + 1);
            resultString = resultString.Insert(startIndex, newString);
    
            char[] originalArray = originalString.ToCharArray();
            char[] newArray = newString.ToCharArray();
            char[] resultlArray = new char[originalArray.Length - (endIndex - startIndex + 1) + newArray.Length];
    
            Array.ConstrainedCopy(originalArray, 0, resultlArray, 0, startIndex);
            newArray.CopyTo(resultlArray, startIndex);
            Array.ConstrainedCopy(originalArray, endIndex + 1, resultlArray, startIndex + newArray.Length, originalArray.Length - endIndex - 1);
    
    打赏 评论

相关推荐 更多相似问题