encodeURIComponent

Summary

Encodes (escapes) an individual URI component, such as a query string.

Usage

public string encodeURIComponent(string uriComponent)

Returns

The escaped URI component.

Parameters

uriComponent

The URI component to encode.

Description

Encodes an individual URI "component" such as:

  • Query strings
  • Protocol
  • Hostname
  • etc.

The following characters will not be encoded: -_.~!*()'

The encoding works by converting characters via UTF-8 encoding. The resulting stream of octets is represented with an escape sequence of the form "%xx". Note that with UTF-8 encoding, the final encoded result can be composed of one, two, three, or four escape sequences depending on the bit combination required to represent the code point.

If the input URI component is invalid, System.Exceptions.InvalidURIException will be thrown. An example of an invalid URI would be a UTF-8 high surrogate without a corresponding low surrogate.

encodeURI vs encodeURIComponent

To understand the differences between encodeURI and encodeURIComponent and to learn the best practices in JS++ when dealing with these methods, please refer to the language guide.

Examples

Encoding a query string
1
2
3
4
5
6
7
import System;
import System.Encoding.URI;
 
string email = "test@email.com"
string encodedEmail = encodeURIComponent(email); // "test%40email.com"
string url = "https://www.test.com/register.jspp?email=" + encodedEmail;
Console.log(url);

Share

HTML | BBCode | Direct Link