替換字串中所有空格為 ‘%20’ 的 C# 程式


我們有一個包含空格的示例字串 −

str ="Hello World !";

使用 C# 中的 Replace() 方法將字串中的所有空格替換為 ‘%20’ −

str2 = str.Replace(" ", "%20");

示例

您可以嘗試執行以下程式碼,將字串中的所有空格替換為 ‘%20’。

實際演示

using System;
class Demo {
   static void Main() {
      String str, str2;
      str ="Hello World !";
      Console.WriteLine("String: "+str);
      str2 = str.Replace(" ", "%20");
      Console.WriteLine("String (After replacing): "+str2);
   }
}

輸出

String: Hello World !
String (After replacing): Hello%20World%20!

更新於: 2020 年 6 月 19 日

2000+ 次瀏覽

開啟你的事業

完成課程,獲得認證

開始
廣告
© . All rights reserved.