zoukankan      html  css  js  c++  java
  • 替换空格

    题目描述

    请实现一个函数,将一个字符串中的每个空格替换成“%20”。
    例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
     1 public class ReplaceSpace {
     2     
     3     public static void main(String[] args) { 
     4         StringBuffer str = new StringBuffer("we er weq eqwqwe df");
     5         String s = replaceSpace(str);
     6         System.out.println(s);
     7     }
     8 
     9     public static String replaceSpace(StringBuffer str) {
    10         String result = str.toString();
    11         if(result.contains(" ")){
    12             while(str.indexOf(" ") != str.lastIndexOf(" ")){
    13             int index = str.indexOf(" ");
    14             str.replace(index,index+1,"%20");
    15         }
    16             int index = str.indexOf(" ");
    17             str.replace(index,index+1,"%20");
    18             result = str.toString();
    19             return result;
    20 
    21         }else{
    22             return result;
    23         }
    24     }
    25 }
  • 相关阅读:
    BZOJ 4525 二分
    BZOJ 4565 状压DP
    BZOJ 3930 容斥原理
    BZOJ 4562 搜索...
    BZOJ 4563 错排+高精度
    BZOJ 1833 数位DP
    BZOJ 4517 组合数+错排
    python 入门学习(二)
    python 入门学习
    Python 爬虫
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11000001.html
Copyright © 2011-2022 走看看