ChatGPT brush force buckle interview questions 01.03. URL

topic description

URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,请使用字符数组实现,以便直接在数组上操作。)

示例 1:

输入:"Mr John Smith    ", 13
输出:"Mr%20John%20Smith"
示例 2:

输入:"               ", 5
输出:"%20%20%20%20%20"
 
提示:
字符串长度在 [0, 500000] 范围内。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/string-to-url-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Start solving problems (JavaScript)

Check step by step

absorb learning

Absorb and learn the slice function and encodeURL function in the JavaScript sample code

slice function

Functions in JavaScript slice()are used to intercept part of an array or string and return a new array or string. slice()The function accepts two parameters: start index and end index (optional). The start index indicates the starting position of the interception (the element containing this position), and the end index indicates the end position of the interception (the element not including this position).

grammar:

  • For arrays:array.slice(start, end)
  • For strings:string.slice(start, end)

Example:

  1. For arrays:
    var array = [1, 2, 3, 4, 5]; var newArray = array.slice(1, 4); console.log(newArray); // output: [2,  3, 4]
  2. For strings:
    var string = "Hello, world!"; var newString = string.slice(7, 12); console.log(newString);  // output: "world"

Precautions:

  • If the end index is omitted, all elements from the start index to the end of the array or string are truncated.
  • If the start index or end index is negative, it indicates the position counted from the end, such as the -1last element.
  • slice()The function does not mutate the original array or string, but returns a new array or string.

It should be noted that there are some subtle differences between array and string interception operations. In strings, slice()the function returns a new string, and in arrays, slice()the function returns a new array.

encodeURIComponent() function

In JavaScript, you can use built-in encodeURIComponent()functions to encode URLs. This function is used to convert special characters in URLs to their encoded representations for safe use in URLs. The encoded URL can decodeURIComponent()be decoded by the function.

grammar:

encodeURIComponent(uri)

Example:

var url = "https://www.example.com/?name=John Smith";
var encodedURL = encodeURIComponent(url);
console.log(encodedURL); // 输出:"https%3A%2F%2Fwww.example.com%2F%3Fname%3DJohn%20Smith"

In the above example, encodeURIComponent()the function encodes special characters in the URL, including colons, slashes, and question marks. The encoded URL can be passed as an argument to other functions or used to build a full URL.

Note that encodeURIComponent()the function encodes all non-alphanumeric characters, including some reserved characters (such as colon, slash, question mark, equal sign) and special characters (such as spaces, quotes, angle brackets, etc.). This means that the encoded URL may be longer than the original URL. And for the encoding of the entire URL, encodeURI()functions can be used.

Guess you like

Origin blog.csdn.net/qq_39154376/article/details/131835887