• 2、替换空格


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

    ============Python============

    # -*- coding:utf-8 -*-
    class Solution:
        # s 源字符串
        def replaceSpace(self, s):
            # write code here
            res = s.split(' ');
            ans = '';
            for i in range(len(res) - 1):
                ans += res[i]
                ans += '%20'
            ans += res[-1]
            return ans

    ================Java==============

    public class Solution {
        public String replaceSpace(StringBuffer str) {
            //遍历一遍字符串找出空格的数量
            if (str == null || str.length() < 0) {
                return null;
            }
            int spacenum = 0;  //计算空格数
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) == ' ')
                    spacenum++;
            }
            int indexold = str.length() - 1;
            int newlength = str.length() + spacenum * 2;
            int indexnew = newlength - 1;
            str.setLength(newlength);
            for (;indexold>=0 && indexold<newlength; --indexold) {
                if (str.charAt(indexold) == ' '){
                    str.setCharAt(indexnew--, '0');
                    str.setCharAt(indexnew--, '2');
                    str.setCharAt(indexnew--, '%');
                } else {
                    str.setCharAt(indexnew--, str.charAt(indexold));
                }
            }
            return str.toString();
        }
    }
  • 相关阅读:
    percona-toolkit
    美河在线
    http://planet.mysql.com/
    MySQL性能诊断与调优 转
    PDB CDB
    mysql安装三 linux源码安装mysql5.6.22
    Solaris10 下mysql5.5.12的安装
    c# 进程间通信
    C# 进程同步,通信
    有关DotNetBar设计样式和运行时的样式不一致的问题
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13537810.html
Copyright © 2020-2023  润新知