Html中連續半形空格的正常顯示

HIYEE發表於2008-09-24
Html對於連續的幾個半形空格,只顯示最開始的一個。如果要在網頁上嚴格的現實所有的半形空格必須對輸入在網頁上的字串做處理。方法是:將用全形代替兩個半形....下面是C#描述。


  1. private static  string  GetFixedStr (string str)
  2.         {
  3.             string outPut="";
  4.             char[] charA= str.ToCharArray();
  5.             int spaceCount = 0;
  6.             List <int> listTr = new List<int>() ;
  7.             
  8.             for (int i = 0; i < str.Length; i++)
  9.             {
  10.                 byte[] arrayByte = new byte[1];
  11.                 arrayByte = System.Text.Encoding.ASCII.GetBytes(charA[i].ToString());
  12.                 int assicCode = (short)(arrayByte[0]);
  13.                 //空格
  14.                 if (assicCode == 32)
  15.                 {
  16.                     spaceCount++;
  17.                     listTr.Add(i);
  18.                 }
  19.                 if (assicCode != 32 || i == str.Length - 1) 
  20.                 {
  21.                     if (spaceCount > 0)
  22.                     {
  23.                         //偶數
  24.                         if (spaceCount % 2 == 0)
  25.                         {
  26.                             for (int j = 0; j < spaceCount / 2; j++)
  27.                             {
  28.                                 charA[listTr[listTr.Count - 1]] = ' ';
  29.                                 listTr.RemoveAt(listTr .Count -1);
  30.                             }
  31.                         }
  32.                         //奇數
  33.                         else
  34.                         {
  35.                             //先刪除一個空格
  36.                             listTr.RemoveAt(listTr.Count - 1);
  37.                             for (int j = 0; j < spaceCount / 2; j++)
  38.                             {
  39.                                 charA[listTr[listTr.Count - 1]] = ' ';
  40.                                 listTr.RemoveAt(listTr.Count - 1);
  41.                             }
  42.                         }
  43.                         spaceCount = 0;
  44.                     }
  45.                 }
  46.             }
  47.             for (int i2 = 0; i2 < charA.Length; i2++)
  48.             {
  49.                 if (listTr.Contains(i2)==false)
  50.                 {
  51.                     outPut += charA[i2].ToString();
  52.                 }
  53.             }
  54.             return outPut;
  55.         }
 

相關文章