---
title: "按照UTF-8编码和解码"
author: "hlbng"
date: "2009-06-01T09:26:33Z"
tags: []
slug: "utf-8"
draft: false
lastmod: "2009-06-01T09:26:33Z"
id: 399400
url: "https://hlbng.iteye.com/blog/399400"
---

```
// 按照utf-8的格式解码
public static String decodeStringByUTF8(String str) {
	if (isBlank(str))
		return "";
	try {
		return URLDecoder.decode(str, "utf-8");
	} catch (UnsupportedEncodingException e) {
	}
	return "";
}

// 按照utf-8的格式编码
public static String encodeStringByUTF8(String str) {
	if (isBlank(str))
		return "";
	try {
		return URLEncoder.encode(str, "utf-8");
	} catch (UnsupportedEncodingException e) {
	}
	return "";
}

// 判断是字符串否为空
public static boolean isBlank(String str) {
	return (str == null || str.trim().equals(""));
}
```