JAVA全角转半角, 半角转全角

package com.test;

public class test {
	public static void main(String[] args) {
		String QJstr = "wchAAAAAAAAAAAAA";
		String QJstr1 = "helloDFGFDGDFGDGDFGDFGDFGDFG";

		String result = ToSBC(QJstr);
		String result1 = ToDBC(QJstr1);
		System.out.println(QJstr + "\n" + result);
		System.out.println(QJstr1 + "\n" + result1);
	}

	/**
	 * 半角转全角
	 * @param input
	 * @return
	 */
	public static String ToSBC(String input) {
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == ' ') {
				c[i] = '\u3000';
			} else if (c[i] < '\177') {
				c[i] = (char) (c[i] + 65248);

			}
		}
		return new String(c);
	}

	/**
	 * 全角转半角
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input) {

		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\u3000') {
				c[i] = ' ';
			} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
				c[i] = (char) (c[i] - 65248);

			}
		}
		String returnString = new String(c);

		return returnString;
	}

}

你可能感兴趣的:(全角转半角)