Html中连续半角空格的正常显示

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.         }
 

你可能感兴趣的:(.NET)