Java 替换“&引用;带“quot;”的字符串的“20”条复杂性问题,下面提到的两个问题中哪一个应该优先考虑? 将其转换为字符数组,然后将其连接回去,并用“%20”替换空格

Java 替换“&引用;带“quot;”的字符串的“20”条复杂性问题,下面提到的两个问题中哪一个应该优先考虑? 将其转换为字符数组,然后将其连接回去,并用“%20”替换空格,java,string,Java,String,或 将字符串划分为以“空格”作为“分隔符”的子字符串,并将字符串与它们之间的“%20”组合 例如: Str = "This is John Shaw " (字符串末尾的额外空格数与字符串中的空格数相同) 预期成果: "This%20is%20John%20Shaw" 不是这个吗 txt = txt.replaceAll(" ", "%20"); 如果我理解错了,请告诉我。使用String类的replaceAll方法,如下所示 String str = "This is John Sha

  • 将字符串划分为以“空格”作为“分隔符”的子字符串,并将字符串与它们之间的“%20”组合
  • 例如:

    Str = "This is John Shaw   "
    
    (字符串末尾的额外空格数与字符串中的空格数相同)

    预期成果:

    "This%20is%20John%20Shaw"
    
    不是这个吗

    txt = txt.replaceAll(" ", "%20");
    

    如果我理解错了,请告诉我。

    使用String类的replaceAll方法,如下所示

    String str = "This is John Shaw ";
    str = str.replaceAll(" ", "%20");
    
    输出


    这是%20is%20John%20Shaw%20

    您可以编写两种算法的复杂度为O(n),其中n是字符串中的字符数,但有更好的算法可以做到这一点。 顺便说一句,我写了一个例子来说明计算时间,一种方法比另一种方法快,但正如我所说,它们都是O(n)

    公共类复杂性测试仪
    {
    //第一种方法
    公共静态字符串replaceSarray(字符串str)
    {
    str=str.trim();//省略了前导空格和尾随空格
    char[]charArray=str.toCharArray();
    字符串结果=”;
    
    对于(inti=0;i
    foo.replace(“,“%20”)
    但听起来你想对URL进行编码,也许你真的想使用URLEncode:举个例子就好了。@a_horse_,没有名字
    URLEncoder
    conwert witespace into
    +
    @talex:我知道,但如果Type_Caster真的想对URL进行编码,那是更好的选择。请再读一遍!内容和问题uestion已从我之前发布的内容更改…
    replaceAll
    在这里无事可做(您不使用正则表达式),使用
    replace
    (与@faheemfarhan的答案相同)
    replaceAll
    在这里无事可做(您不使用正则表达式),使用
    replace
    @DmitryGinzburg您坐的地方?它是如何工作的?它按预期和要求工作。是的,它将按预期工作,但您确实不需要正则表达式,它被
    replaceAll
    接受,只要
    replace
    就足够了。
    public class ComplexityTester
    {
        //FIRST METHOD
        public static String replaceSpacesArray(String str)
        {
            str = str.trim(); // leading and trailing whitespaces omitted
    
            char[] charArray = str.toCharArray();
    
            String result = "";
    
            for(int i = 0; i<charArray.length; i++) // it replaces spaces with %20
            {
                if(charArray[i] == ' ') //it's a space, replace it!
                result += "%20";        
                else                    //it's not a space, add it! 
                result += charArray[i];
            }
    
            return result;
        }
    
        //SECOND METHOD
        public static String replaceSpacesWithSubstrings(String str)
        {
            str = str.trim(); // leading and trailing whitespaces omitted
    
            String[] words = new String[5]; //array of strings, to add substrings
            int wordsSize = 0;              //strings in the array
    
    
            //From the string to an array of substrings 
            //(the words separated by spaces of the string)
            int indexFrom = 0;              
            int indexTo = 1;
    
            while(indexTo<=str.length())
            {
                if(wordsSize == words.length) //if the array is full, resize it!
                words = resize(words);
    
                //we reach the end of the sting, add the last word to the array!
                if(indexTo == str.length()) 
                {
                    words[wordsSize++] = str.substring(indexFrom, indexTo++);
                }
                else if(str.substring(indexTo-1,indexTo).equals(" "))//it's a space
                {
                    //we add the last word to the array
                    words[wordsSize++] = str.substring(indexFrom, indexTo-1); 
    
                    indexFrom = indexTo; //update the indices
                    indexTo++;
                }
                else //it's a character not equal to space
                {
                    indexTo++;  //update the index
                }
            }
    
            String result = "";
    
            // From the array to the result string
            for(int i = 0; i<wordsSize; i++)
            {
                result += words[i];
    
                if(i+1!=wordsSize) 
                result += "%20";
            }
    
            return result;
        }
    
        private static String[] resize(String[] array) 
        {
            int newLength = array.length*2;
    
            String[] newArray = new String[newLength];
    
            System.arraycopy(array,0,newArray,0,array.length);
    
            return newArray;
        }
    
        public static void main(String[] args)
        {
            String example = "The Java Tutorials are practical guides "
            +"for programmers who want to use the Java programming " 
            +"language to create applications. They include hundreds " 
            +"of complete, working examples, and dozens of lessons. " 
            +"Groups of related lessons are organized into \"trails\"";
    
            String testString = "";
    
            for(int i = 0; i<100; i++) //String 'testString' is string 'example' repeted 100 times
            {
                testString+=example;
            }
    
            long time = System.currentTimeMillis(); 
            replaceSpacesArray(testString); 
            System.out.println("COMPUTING TIME (ARRAY METHOD) = " 
                                  + (System.currentTimeMillis()-time));
    
            time = System.currentTimeMillis();
            replaceSpacesWithSubstrings(testString);
            System.out.println("COMPUTING TIME (SUBSTRINGS METHOD) = "
                                    + (System.currentTimeMillis()-time));
        }
    }