String(byte[],int offset,int length) 用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。
public byte[] getBytes() 方法使用平台默认的字符编码,将当前字符串转化为一个字节数组。public byte[] getBytes(String charsetName) 使用参数指定字符编码,将当前字符串转化为一个字节数组。
String类练习题:
import org.junit.Test; /** 字符串 : * 是内容不可改变的Unicode字符序列 * * 任何对于字符串内容的改变都将会产生新的字符串对象 * * 0123456789 11 14 17 21 25 * " abcXXXyyyZZZ你好吗?我不好qqq " * public int length() // 26 public char charAt(int index) // 获取指定下标处的字符 public char[] toCharArray() // 把字符串转换为真的字符数组 public boolean equals(Object anObject) public int compareTo(String anotherString) public int indexOf(String s) // 判断参数中的子串在本字符串中首次出现的下标 indexOf("qqq"):21, indexOf("好"):15, indexOf("坏"):-1 public int indexOf(String s ,int startpoint) //indexOf("qqq", 10):21, indexOf("好", 18):20 public int lastIndexOf(String s) // lastIndexOf("qqq"):21, lastIndexOf("好"):20 public int lastIndexOf(String s ,int startpoint) // lastIndexOf("qqq", 5):-1, lastIndex("好", 17):15 public boolean startsWith(String prefix)//判断当前串是否是以参数中的子串为开始 statsWith("abc"):false, startsWith(" a"):true public boolean endsWith(String suffix) public String substring(int start,int end)//截取子串包含开始索引,不包含结束索引,获取子串,[start,end) public String substring(int startpoint) public String replace(char oldChar,char newChar) // 替换字符串中的全部老字符为新字符 public String replaceAll(String old,String new) public String trim() // 去除字符串首尾的空白字符 public String concat(String str) // 连接当前串和参数中的串 public String[] split(String regex) // 把字符串以参数中的子串为切割器, 切割成若干部分 split("好"); */ public class StringTest { @Test public void exer7() { String string3 = "张三,30,80;李四,40,70;王五,50,90;赵六,60,100"; String[] arr = string3.split(";"); Person[] persons = new Person[arr.length]; for (int i = 0; i < arr.length; i++) { String[] arr2 = arr[i].split(","); persons[i] = new Person(arr2[0], Integer.parseInt(arr2[1]), Integer.parseInt(arr2[2])); } for (Person person : persons) { System.out.println(person); } } @Test public void test6() { String string = "100,200,300,400,500,600"; String[] arr = string.split(","); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } String string2 = "张三,30,80"; String[] arr2 = string2.split(","); Person person = new Person(arr2[0], Integer.parseInt(arr2[1]), Integer.parseInt(arr2[2])); System.out.println(person); } //模拟一个trim方法,去除字符串两端的空白字符。 @Test public void exer4() { String string = " a "; int begin = -1; int end = string.length() - 1; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) > 32) { begin = i; break; } } for (int i = string.length() - 1; i >= 0; i--) { if (string.charAt(i) > 32) { end = i; break; } } if (begin == -1) { System.out.println(""); } else { String result = string.substring(begin, end + 1); System.out.println(result); } } @Test public void test5() { String string = " \n \t\t abcXXXyyyZZZ你好吗?我不好qqq \t\r\n"; System.out.println(string.substring(10, 21)); // 截取从Z开始到末尾 System.out.println(string.substring(11, string.length())); System.out.println(string.substring(11)); // 和上面的效果一样 String newStr = string.replace(' ', '*'); System.out.println(newStr); String newStr2 = string.replaceAll("好", "坏"); System.out.println(newStr2); String newStr3 = string.trim(); System.out.println(newStr3); String newStr4 = newStr3.concat("100"); System.out.println(newStr4); } @Test // 反例 public void test4() { String string = "abc"; for (int i = 0; i < 10000000; i++) { string += i; } System.out.println(string); } /** * 求短串在长串中出现的次数 * @param string1 * @param string2 * @return */ public int getCountOfSubstring(String string1, String string2) { if (string2.length() > string1.length()) { String tmp = string1; string1 = string2; string2 = tmp; } int count = 0; int index = 0; while (true) { // 去尝试查找子串 index = string1.indexOf(string2, index); if (index == -1) { break; } count++; index++; } return count; } @Test public void exer3() { /*获取一个字符串在另一个字符串中出现的次数。 比如:获取"ab"在 "abkkcadkabkebfkabkskab" 中出现的次数*/ String string1 = "aaaaa"; String string2 = "aa"; int count = getCountOfSubstring(string2, string1); System.out.println(count); } @Test public void test3() { //String(char[] value, int offset, int count) char[] arr = {'a','b','你','他','嘿','来'}; String string = new String(arr); System.out.println(string); String string2 = new String(arr, 2, 3); // 2是开始下标,3是总共的字符数 System.out.println(string2); String string3 = new String("abcccc"); System.out.println(string3); String string4 = new String(arr, 0, arr.length); System.out.println(string4); } @Test public void test2() { String string = " abcXXXyyyZZZ你好吗?我不好qqq "; System.out.println(string.length()); System.out.println(string.charAt(18)); System.out.println("--------------------"); for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); System.out.println(ch); } // 把字符串变成全部大写的 } @Test public void exer2() { // 把字符串反转成新的字符串 String string = " abcXXXyyyZZZ你好吗?我不好qqq "; String newStr = ""; for (int i = string.length() - 1; i >= 0; i--) { newStr += string.charAt(i); } System.out.println(newStr); } @Test public void exer22() { // 把字符串反转成新的字符串 String string = " abcXXXyyyZZZ你好吗?我不好qqq "; String newStr = ""; for (int i = 0; i < string.length(); i++) { newStr = string.charAt(i) + newStr; } System.out.println(newStr); } @Test public void exer23() { // 把字符串反转成新的字符串 String string = " abcXXXyyyZZZ你好吗?我不好qqq "; char[] arr = string.toCharArray(); for (int i = 0; i < arr.length / 2; i++) { char tmp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = tmp; } String newStr = new String(arr); System.out.println(newStr); } @Test public void exer1() { // 把字符串变成全部大写的 String string = " abcXXXyyyZZZ你好吗?我不好qqq "; String newStr = ""; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if (ch >= 'a' && ch <= 'z') { // 变成大写 ch -= 32; } newStr += ch; } System.out.println(newStr); } @Test public void test1() { String string = "abc"; string += 100; // string = string + 100; System.out.println(string); } } public class Person { private String name; private int age; private double weight; public Person() { } public Person(String name, int age, double weight) { super(); this.name = name; this.age = age; this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", weight=" + weight + "]"; } } package com.guigu.javase.home; import org.junit.Test; public class HomeWork { @Test public void work1() { String path = "D:\\MyWork\\jdk1.7.0_07\\bin;c:\\gtk\\bin;c:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\bin;c:\\gtk\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\OpenCL SDK\\3.0\\bin\\x86;C:\\Program Files (x86)\\Intel\\OpenCL SDK\\3.0\\bin\\x64;;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;D:\\MyStudy\\maven\\apache-maven-3.3.9\\bin"; String[] pathArr = path.split(";"); for (String string : pathArr) { System.out.println(string); } } public String partReverse(String string, int begin, int end) { String str1 = string.substring(0, begin); String str2 = string.substring(begin, end); String str3 = string.substring(end); str2 = new StringBuilder(str2).reverse().toString(); return (str1 + str2 + str3); } @Test public void work2() { //将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为”abfedcg” System.out.println(partReverse("abcdefg22222cccc", 3, 6)); } @Test public void work3() { //获取两个字符串中最大相同子串。比如: String str1 = "abcwerthelloyuiodef"; String str2 = "cvhellobnm"; System.out.println(getMaxSameSub(str2, str1)); } public String getMaxSameSub(String str1, String str2) { int maxSubLen = str2.length(); while (maxSubLen > 0) { for (int i = 0; i + maxSubLen <= str2.length(); i++) { String subString = str2.substring(i, i + maxSubLen); if (str1.indexOf(subString) != -1) { return subString; } } maxSubLen--; } return ""; } }