首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

API练习题-String

2013-12-26 
API练习-Stringpublic class TestString {public static void main(String[] args) {String str1 a4755

API练习-String
public class TestString {public static void main(String[] args) {String str1 = "a4755759";String str2 = "你a好b中012325国!\u4f60";char dst[] = new char[10];/* * 根据索引获取字符,下标从0开始 */System.out.println("1:" + str1.charAt(1));/* * 返回该字符在unicode编码中位置的值 */System.out.println("2:" + str2.codePointAt(3));/* * 返回指定索引之前的字符的Unicode 代码点,其实就是在取3这个下标处的Unicode值 */System.out.println("3:" + str2.codePointBefore(4));/* * 准确计算unicode字符的数量,而不是char的数量,其实就是在计算有多少个字符,如果遇到字符串中存在unicode编码的 * 字符,那会进行转换并解析成一个字符。例如\u4f60就表示一个字符。 */System.out.println("4:" + str2.codePointCount(0, str2.length()-1));/* * 返回指数在此字符串偏离给定的索引codePointOffset代码点 */System.out.println("5:" + str2.offsetByCodePoints(0, 3));/* * 将字符从此字符串复制到目标字符数组,以下代码含义是从字符串的0位开始到第5位结束,把这段字符分别复制到 * dst字符数组里面去,从数组的0位开始粘贴。 */str2.getChars(0, 5, dst, 0);System.out.println("6:" + new String(dst));/* * 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中 * 放进去的值是该字符在该编码下的编号值,比如a在ISO8859-1下的值是97 */byte b1[];try {b1 = "abcdefg".getBytes("ISO8859-1");for (int i = 0; i < b1.length; i++) {System.out.println("7:b1[" + i + "]=" + b1[i]);}} catch (UnsupportedEncodingException e) {e.printStackTrace();}/* * 将此字符串与指定的对象比较 */System.out.println("8:" + str1.equals(str2));/* * 将一个普通的字符串与 StringBuffer 字符串比较,如果你只是用equals来比较,其结果是不正确的。 */System.out.println("9:" + str1.contentEquals(new StringBuffer("a4755759")));/* * 忽略大小写比较 */System.out.println("10:" + str1.equalsIgnoreCase("A4755759"));/* * 按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值 * 两个字符的unicode值不一样时返回相差的值,反之返回0 */System.out.println("11:" + "a".compareTo("A"));/* * 按字典顺序比较两个字符串,不考虑大小写 */System.out.println("12:" + str1.compareToIgnoreCase("A4755759"));/* * 测试两个字符串区域是否相等 * 将此 String 对象的一个子字符串与参数 other 的一个子字符串进行比较。如果这两个子字符串表示相同的字符序列 * ,则结果为 true。 */System.out.println("13:" + str1.regionMatches(true, 0, "A4755759", 0, 2));/* * 测试此字符串从指定索引开始的子字符串是否以指定前缀开始 */System.out.println("14:" + str1.startsWith("4", 1));/* * 测试此字符串是否以指定的后缀结束 */System.out.println("15:" + str1.endsWith("59"));/* * 返回此字符串的哈希码 */System.out.println("16:" + str2.hashCode());/* * 返回指定字符在此字符串中第一次出现处的索引 */System.out.println("17:" + str2.indexOf(98));System.out.println("18:" + str2.indexOf("5", 7));System.out.println("19:" + str2.indexOf("5"));System.out.println("20:" + str2.lastIndexOf("5"));/* * 截取字符 */System.out.println("21:" + str1.substring(3));System.out.println("22:" + str1.substring(3, 4));System.out.println("23:" + str1.subSequence(3, 4));/* * 将指定字符串连接到此字符串的结尾 */System.out.println("24:" + "a".concat(str1));/* * 字符替换:用 newChar 替换此字符串中出现的所有 oldChar */System.out.println("25:" + "This is a dog!".replace("a", "an"));System.out.println("26:" + "This is a dog!".replace(" ", "_"));System.out.println("27:" + "This is a dog!".replace(" ", ""));/* * 告知此字符串是否匹配给定的正则表达式 */System.out.println("28:" + "hell".matches("\\p{Lower}*"));/* * 实际上就是在调indexOf来判断是否存在指定的字符 */System.out.println("29:" + str1.contains("59"));/* * 根据正则表达式去替换匹配到的字符 */System.out.println("30:" + "This is a dog!".replaceFirst("\\b\\w{2}\\b", "an"));/* * 根据匹配给定的正则表达式来拆分此字符串 */String str[] = "This is a dog!".split(" ", 2);for (int i = 0; i < str.length; i++) {System.out.println("31:" + str[i]);}/* * 字母大小写转换 */System.out.println("32:" + "This is a dog".toLowerCase());System.out.println("33:" + "This is a dog".toLowerCase(Locale.getDefault()));System.out.println("34:" + "this is a dog".toUpperCase());System.out.println("35:" + "this is a dog".toUpperCase(Locale.US));/* * 返回字符串的副本,忽略前导空白和尾部空白 */System.out.println("36:" + " This is a dog! ".trim().toString());/* * 字符串转换为一个新的字符数组 */char c1[] = str2.toCharArray();int i = 0;for (char c : c1) {i += 1;System.out.println("37" + i + "=" + c);}/* * 返回 char 数组参数的字符串表示形式 */System.out.println("38:" + String.valueOf(c1));System.out.println("39:" + String.valueOf(c1, 0, 2));/* * 返回 char 数组参数的字符串表示形式 */System.out.println("40:" + String.copyValueOf(c1));/* * 返回 boolean 参数的字符串表示形式 */boolean flag = true;System.out.println("41:" + String.valueOf(flag));/* * 返回 int/long 参数的字符串表示形式 */long abc = 1234564L;System.out.println("42:" + String.valueOf(abc));float f = 123.456F;System.out.println("43:" + String.valueOf(f));/* * 将一个字符中的中文、字母、数字分离出来 */String str3 = "兴44abc中1.q 我中华!";char cr[] = str3.toCharArray();// 1、将字符串转换成char字符数组for (char c : cr) {if (("" + c).getBytes().length > ("" + c).length()) {// 2、判断是否含有中文、字母、数字System.out.println("中文-->" + c);} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {System.out.println("字母-->" + c);} else if (c >= '0' && c <= '9') {System.out.println("数字-->" + c);}}}}

?

热点排行