Simple character substitution in Java

Source: Internet
Author: User

In network programming, assuming that the URL contains special characters, such as spaces, ' # ', and so on, the server will not be able to recognize that the correct number of parameters cannot be obtained. We need to convert these special characters into characters that the server can recognize, such as converting a space into '%20 '. Given a string, convert the spaces into '%20 '. Require as high a efficiency as possible. As far as the complexity of O (n) is complete, a char array is required to store the characters, not string

Input: An original string, such as Hello World.

Output the converted string, such as Hello%20world.



Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner Scanner = new Scanner (system.i n);        String str = scanner.nextline ();        char[] Chararr = Str.tochararray (); int size = Chararr.length;        for (char _char:chararr) {            if (_char = =) {                size + = 2;            }        }        char[] NewArr = new Char[size];        for (int i = 0, j = 0; i < chararr.length; i++) {            if (' '! = Chararr[i]) {                newarr[j++] = Chararr[i];            } el SE {                newarr[j++] = '% ';                Newarr[j++] = ' 2 ';                newarr[j++] = ' 0 ';            }        }        System.out.println (New String (NEWARR));}}



Simple character substitution in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.