当前位置: 代码迷 >> J2SE >> J2SE基础/字符串有关
  详细解决方案

J2SE基础/字符串有关

热度:3380   发布时间:2013-02-25 00:00:00.0
J2SE基础/字符串相关

?

/**	 * 反转字符串	 * eg:"abc" => "cba"	 * @param str	 */	static void reverseString(String str) {		// 目标字符串转换为字符数组		char[] charArray = str.toCharArray();		// 获取转换后的字符数组长度		int charLength = charArray.length;		char char_temp;// 临时字符变量		// 反转后的字符串		String newStr;		// 循环反转字符		for (int i = 0; i < charLength / 2; i++) {			// 将索引[i]元素的值赋值临时字符			char_temp = charArray[i];			// 索引[charLength - 1 - i]元素的值赋值索引[i]			charArray[i] = charArray[charLength - 1 - i];			// 临时字符赋值索引[charLength - 1 - i]元素			charArray[charLength - 1 - i] = char_temp;		}		// 获取反转后的字符串		newStr = String.valueOf(charArray);		// CSharp:newStr = new string(charArray);		// js:newStr = str.split("").reverse().join("");		System.out.println(newStr);	}	static void reverseString2(String str) {		int length = str.length();		StringBuilder sb = new StringBuilder(length);		length--;		for (int i = length; i >= 0; i--) {			sb.append(str.charAt(i));		}		System.out.println(sb.toString());	}
/**	 * 两字符串求最长公共字符串	 * 	 * @param a	 * @param b	 */	static void compareString(String a, String b) {		int beginIndex = 0;		int endIndex = 0;		int maxLength = 0;		String shortString = null;		String longString = null;		if (a.length() > b.length()) {			shortString = b;			longString = a;		} else {			shortString = a;			longString = b;		}		String splitString = null;		for (int i = 0; i <= shortString.length(); i++) {			for (int j = i + 1; j <= shortString.length(); j++) {				splitString = shortString.substring(i, j);				if (longString.indexOf(splitString) > -1) {					if (splitString.length() > maxLength) {						maxLength = splitString.length();						beginIndex = i;						endIndex = j;					}				}			}		}		System.out.println(String.format("The MaxLength:%d", maxLength));		System.out.println(String.format("The BeginIndex:%d", beginIndex));		System.out.println(String.format("The EndIndex:%d", endIndex));		System.out.println(String.format("The MaxString:%s",				a.substring(beginIndex, endIndex)));	}	/*	 * 判断回文数	 */	static void isPalindrome(String str) {		boolean sign = true;		int length = str.length();		int start = 0;		int end = 0;		if (length == 1)			sign = false;		for (int i = 0; i < length / 2; i++) {			start = str.charAt(i);			end = str.charAt(length - i - 1);			if ((start ^ end) != 0)				sign = false;			// return		}		System.out.println(String.format("isPalindrome: %s", sign));	}	/*	 * 判断某一字符串是否与另一字符串相同	 */	static void isAnagrams(String a, String b) {		int aLength = a.length();		int bLength = b.length();		int aChar = 0;		int bChar = 0;		boolean sign = true;		int result = 0;		if (aLength != bLength)			sign = false;		// return		for (int i = 0; i < aLength; i++) {			aChar = a.charAt(i);			bChar = b.charAt(i);			result ^= aChar ^ bChar;		}		sign = result == 0;		System.out.println(String.format("isAnagrams: %s", sign));	}	/*	 * 截取中英文混合的字符串	 */	static void subString(String str, int maxLen) {		int len = 0;		char cha = '0';		StringBuilder sb = new StringBuilder(maxLen);		if (str == null || str.isEmpty())			return;		try {			for (int i = 0; i < str.length(); i++) {				cha = str.charAt(i);				len += String.valueOf(cha).getBytes("GBK").length;				if (len > maxLen)					break;				sb.append(cha);			}		} catch (UnsupportedEncodingException e) {			e.printStackTrace();		}		System.out.println(sb.toString());	}	/*	 * 汉字九九乘法表	 */	static void multiplicationTable() {		char[] chars = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十' };		int result = 0;		for (int i = 1; i <= 9; i++) {			for (int j = 1; j <= i; j++) {				result = i * j;				if (String.valueOf(result).length() == 1) {					System.out.print(String.format("  %s×%s=%s  ", chars[i],							chars[j], chars[result]));				} else {					if (result % 10 == 0) {						System.out.print(String.format("  %s×%s=%s%s  ",								chars[i], chars[j], chars[(result / 10)],								chars[10]));					} else {						System.out.print(String.format("  %s×%s=%s%s%s  ",								chars[i], chars[j], chars[(result / 10)],								chars[10], chars[(result % 10)]));					}				}			}			System.out.println();		}	}	/*	 * 模拟手算加法实现大数字计算	 */	static String addString(String a, String b) {		// 判断是否为null		if (a == null || b == null)			return null;		String _a = a.trim();		String _b = b.trim();		// 判断是否为空		if (_a.isEmpty() && _b.isEmpty())			return null;		// 判断是否为0		if (_a.equals("0"))			return _b;		if (_b.equals("0"))			return _a;		// 判断是否为数字		if (!(StringUtils.isNumber(a) && StringUtils.isNumber(b)))			return null;		// 不足位补零		if (_a.length() > _b.length()) {			String temp = _a;			_a = _b;			_b = temp;		}		_a = StringUtils.addZeroToFirst(_a, _b.length());		// 定义返回值变量		StringBuilder result = new StringBuilder(_a.length());		// 循环模拟进位相加		int sum = 0;		int temp = 0;		int num1 = 0;		int num2 = 0;		for (int i = _a.length() - 1; i >= 0; i--) {			num1 = _a.charAt(i) - '0';			num2 = _b.charAt(i) - '0';			sum = num1 + num2 + temp;			result.append(sum % 10);			temp = sum / 10;		}		// 反转字符串		result.reverse();		// 判断最高位相加的和		if (temp != 0)			result.insert(0, temp);		return result.toString();	}