XhCode Online Converter Tools

Java / .Net Escape Unescape



Done Code:
Java / .Net Escape Unescape Online Converter

To implement escape/unescape functionality in Java and .NET, you can use the following methods:

Java Escape/Unescape
In Java, you typically use classes like URLEncoder and URLDecoder to handle escaping and unescaping for URLs. For other types of escaping (like HTML), you might use libraries such as Apache Commons Lang.

URL Encoding/Decoding
Escape (Encode):

Use URLEncoder to encode strings, making them URL-safe.
java

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class EscapeExample {
public static void main(String[] args) throws UnsupportedEncodingException {
String originalString = "Hello, world!";
String encodedString = URLEncoder.encode(originalString, "UTF-8");
System.out.println(encodedString); // Output: Hello%2C+world%21
}
}
Unescape (Decode):

Use URLDecoder to decode URL-encoded strings.
java

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class UnescapeExample {
public static void main(String[] args) throws UnsupportedEncodingException {
String encodedString = "Hello%2C+world%21";
String decodedString = URLDecoder.decode(encodedString, "UTF-8");
System.out.println(decodedString); // Output: Hello, world!
}
}
HTML Encoding/Decoding (Apache Commons Lang)
For HTML escaping, use StringEscapeUtils from the Apache Commons Lang library.

Escape HTML:

java

import org.apache.commons.text.StringEscapeUtils;

public class EscapeHtmlExample {
public static void main(String[] args) {
String text = "<div>Welcome</div>";
String escapedHtml = StringEscapeUtils.escapeHtml4(text);
System.out.println(escapedHtml); // Output: &lt;div&gt;Welcome&lt;/div&gt;
}
}
Unescape HTML:

java

import org.apache.commons.text.StringEscapeUtils;

public class UnescapeHtmlExample {
public static void main(String[] args) {
String escapedHtml = "&lt;div&gt;Welcome&lt;/div&gt;";
String unescapedHtml = StringEscapeUtils.unescapeHtml4(escapedHtml);
System.out.println(unescapedHtml); // Output: <div>Welcome</div>
}
}
.NET Escape/Unescape
In .NET, you can use HttpUtility (from the System.Web namespace) for URL encoding/decoding and HTML encoding/decoding.

URL Encoding/Decoding
Escape (Encode):

csharp

using System;
using System.Web;

public class EscapeExample
{
public static void Main()
{
string originalString = "Hello, world!";
string encodedString = HttpUtility.UrlEncode(originalString);
Console.WriteLine(encodedString); // Output: Hello%2c+world%21
}
}
Unescape (Decode):

csharp

using System;
using System.Web;

public class UnescapeExample
{
public static void Main()
{
string encodedString = "Hello%2c+world%21";
string decodedString = HttpUtility.UrlDecode(encodedString);
Console.WriteLine(decodedString); // Output: Hello, world!
}
}
HTML Encoding/Decoding
Escape HTML:

csharp

using System;
using System.Web;

public class EscapeHtmlExample
{
public static void Main()
{
string text = "<div>Welcome</div>";
string escapedHtml = HttpUtility.HtmlEncode(text);
Console.WriteLine(escapedHtml); // Output: &lt;div&gt;Welcome&lt;/div&gt;
}
}
Unescape HTML:

csharp

using System;
using System.Web;

public class UnescapeHtmlExample
{
public static void Main()
{
string escapedHtml = "&lt;div&gt;Welcome&lt;/div&gt;";
string unescapedHtml = HttpUtility.HtmlDecode(escapedHtml);
Console.WriteLine(unescapedHtml); // Output: <div>Welcome</div>
}
}
Summary
Java: Use URLEncoder/URLDecoder for URL encoding/decoding. For HTML escaping, use StringEscapeUtils from Apache Commons Lang.
.NET: Use HttpUtility.UrlEncode/UrlDecode for URL encoding/decoding. Use HttpUtility.HtmlEncode/HtmlDecode for HTML escaping/unescaping.

TOP