替换空格

实现一个函数,将一个字符串中的每个空格替换成"%20"。例如,当字符串为"We Are Happy.",则经过替换之后的字符串为"We%20Are%20Happy."。

 1 using System.Text;
 2 
 3 namespace JianZhiOffer
 4 {
 5     class ReplaceSpace
 6     {
 7         public string replaceSpace(string str)
 8         {
 9             // write code here
10             if (str == null)
11             {
12                 return null;
13             }
14 
15             StringBuilder strBuff = new StringBuilder();
16 
17             for (int i = 0; i < str.Length; i++)
18             {
19                 if (str[i] == ' ')
20                 {
21                     strBuff.Append(@"%20");
22                 }
23                 else 
24                 {
25                     strBuff.Append(str[i]);
26                 }
27             }
28 
29             return strBuff.ToString();
30         }
31     }
32 }
原文地址:https://www.cnblogs.com/xiaolongren/p/11809468.html