日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)編程開發(fā)C#.NET → .net中直接修改臨時(shí)字符串

      .net中直接修改臨時(shí)字符串

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來源:西西整理時(shí)間:2012/5/10 23:00:59字體大。A-A+

      作者:佚名點(diǎn)擊:16次評(píng)論:1次標(biāo)簽: 字符串

      • 類型:電子教程大。9.5M語(yǔ)言:中文 評(píng)分:8.0
      • 標(biāo)簽:
      立即下載

      有時(shí)候我們需要對(duì)字符串進(jìn)行修改,但是.net里面的字符串是只讀的,修改動(dòng)作會(huì)產(chǎn)生一個(gè)新的字符串,多數(shù)時(shí)候這都不是我想要的結(jié)果。不過fixed還是可以幫我們解決一部分問題的,比如.ToLower、.ToUpper、.Replace(char, char)等等,不過有個(gè)限制,就是變換前后的字符串長(zhǎng)度不變。

              public unsafe static string toLower(this string value)
              {
                  if (value != null)
                  {
                      fixed (char* valueFixed = value) toLowerUnsafe(valueFixed, valueFixed + value.Length);
                  }
                  return value;
              }
              public unsafe static void toLower(char* start, char* end)
              {
                  if (start != null && end > start) toLowerUnsafe(start, end);
              }
              public unsafe static void toLowerUnsafe(char* start, char* end)
              {
                  while (start != end)
                  {
                      if ((uint)(*start - 'A') < 26) *start |= (char)0x20;
                      ++start;
                  }
              }

              public unsafe static string toUpper(this string value)
              {
                  if (value != null)
                  {
                      fixed (char* valueFixed = value) toUpperUnsafe(valueFixed, valueFixed + value.Length);
                  }
                  return value;
              }
              public unsafe static void toUpper(char* start, char* end)
              {
                  if (start != null && end > start) toUpperUnsafe(start, end);
              }
              public unsafe static void toUpperUnsafe(char* start, char* end)
              {
                  while (start != end)
                  {
                      if ((uint)(*start - 'a') < 26) *start -= (char)0x20;
                      ++start;
                  }
              }

              public static string replace(this string value, char oldChar, char newChar)
              {
                  return value != null && value.Length != 0 ? replaceUnsafe(value, oldChar, newChar) : null;
              }
              public unsafe static string replaceNotNull(this string value, char oldChar, char newChar)
              {
                  return value.Length != 0 ? replaceUnsafe(value, oldChar, newChar) : value;
              }
              public unsafe static string replaceUnsafe(this string value, char oldChar, char newChar)
              {
                  fixed (char* valueFixed = value)
                  {
                      char* start=valueFixed, end = valueFixed + value.Length;
                      char endValue = *--end;
                      *end = oldChar;
                      do
                      {
                          while (*start != oldChar) ++start;
                          *start = newChar;
                      }
                      while (start++ != end);
                      if (endValue != oldChar) *end = endValue;
                  }
                  return value;
              }

      Release實(shí)測(cè),效果還不錯(cuò),英文大小寫轉(zhuǎn)換運(yùn)行時(shí)間降到了.ToLower與.ToUpper的1/4以下,字符替換運(yùn)行時(shí)間降到了.Replace(char, char)的1/5以下,同時(shí)給垃圾回收也減少了很多壓力。當(dāng)然.ToLower與.ToUpper還是比較強(qiáng)大的,它不僅僅只是英文轉(zhuǎn)換,但是我個(gè)人只處理過英文大小寫問題。
      需要注意的是,這里是直接修改字符串的數(shù)據(jù),會(huì)影響到所有相關(guān)引用,如果對(duì)影響程度沒有把握,一般建議應(yīng)用在一次性的臨時(shí)變量上。這里只是舉幾個(gè)例子,具體其它可能的字符串修改應(yīng)用就需要你自己卻發(fā)掘了。

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(1)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)