1. 构造对象:
String对象创建的对象具有两个基本值:引用、实体; 1、String常量放在常量池中,常量池中的数据在程序运行期间不允许再次改变; 2、String对象变量中存放着引用,new后首先分配内存空间并在内存空间中放入字符序列,然后计算出引用; 3、new出的String对象放在动态区中,而非常量池中,尽管在实体上一样,但引用不同;
2. 字符串并置(String + String):
public class Example8_1 {
public static void main(String[] args) {
String a0 = "java";
String a1 = "ja" + "va";
String a11 = new String("java");
System.out.println(a0 == a1); // true
System.out.println(a0 == a11); // false
System.out.println("java" == a0); // true
String a = "ja";
String b = "va";
String c = a + b; // 并置运算
System.out.println(a0 == c); // false
String a2 = a + b;
System.out.println(a2 == c); // false
}
}
3. public boolean equals(String s) || startsWith(String s,int toffset) || startsWith(String s) || endsWith(String s):
- equals(String s)方法比较的是当前String对象的字符序列;
- equalsIgnoreCase(String s)方法忽略大小写的字符序列进行比较;
- startsWith(String s),判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列;
- startsWith(String s,int toffset),从第toffset位的位置开始查找;
- endsWith(String s),判断当前String对象的字符序列后缀是否是参数指定的String对象s的字符序列.
public class Example8_2 {
public static void main(String[] args) {
String a0 = "java";
String a1 = a0 + "";
System.out.println(a1.equals(a0)); // false
String a3 = new String("java1");
String a4 = "Java1";
System.out.println(a3.equalsIgnoreCase(a4)); // true
String a11 = "坚持学习java基础";
String a12 = "好好学习,天天向上";
System.out.println(a11.startsWith("坚持")); // true
System.out.println(a12.startsWith("不好好")); // false
System.out.println(a11.startsWith("学", 2)); // true 0->坚
System.out.println(a12.startsWith("向上", 8)); // false 8->上
System.out.println(a11.endsWith("坚持学习")); // false
System.out.println(a12.endsWith("天天向上")); // true
}
}
4. public int compareTo(String s):
String对象调用compareTo(String s)方法时,按字典序与参数指定的String对象s的字符串序列进行比较大小,
若相同,则返回值为0;
若大于s的字符序列,则返回正数数值;
若小于s的字符序列,则返回负数数值。
public class Example8_4 {
public static void main(String[] args) {
String[] a = {"java", "c", "c#", "c++", "python"};
String[] b = {"String", "Integer", "Long", "Double", "Date"};
System.out.println("使用SortString类方法按字典序进行排列:");
SortString.sortDictionary(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println("\n使用Arrays类方法按字典序进行排列:");
Arrays.sort(b);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
class SortString {
public static void sortDictionary(String[] args) {
String middle = null;
// 最大循环次数
for (int i = 0; i <= args.length; i++) {
// 单个数值对所有数值的最大比较次数
for (int j = i + 1; j < args.length; j++) {
// 若args[j] > args[i],则返回正值大于0,相反小于0
if (args[j].compareTo(args[i]) < 0) {
// 进行数值替换,实现最小的放在第一位
middle = args[i];
args[i] = args[j];
args[j] = middle;
}
}
}
}
}
5. public boolean contains(CharSequece s):
判断当前String对象s的字符序列是否包含CharSequece参数s的字符序列
System.out.println("索引位置都从0开始!!");
String s = "student";
System.out.println(s.contains("stt")); // false
6. public int indexOf(String strFirst)、public int lastIndexOf(String s):
String对象从当前String对象的字符序列的0索引位置开始检索首次出现strFirst的字符序列的位置,并返回该位置,若没有检索到,则返回 -1 ;
String strFirst = "from zero,get string and return it\'s string location";
System.out.println(strFirst.indexOf("string")); // 14
System.out.println(strFirst.indexOf("string123")); // -1
System.out.println(strFirst.indexOf("string", 20)); // 37
String对象从当前String对象的字符序列的0索引位置开始检索最后一次出现strFirst的字符序列的位置,并返回该位置,若没有检索到,则返回 -1 .
String strLast = "from last,get string and return it\'s string location";
System.out.println(strLast.lastIndexOf("string")); // 37
System.out.println(strLast.lastIndexOf("string", 15)); // 14
7. public String subString(int startpoint) ---- (int start,int end):
获取一个String对象的字符序列中从startpoint位置至最后位置上的字符所得到的的字符序列 ---- 从start到end - 1的位置;
String strSub = " + Operation - ";
System.out.println(strSub.substring(3)); //Operation -
System.out.println(strSub.substring(3, 12)); //Operation
8. public String trim():
获取新的当前String对象的字符序列去掉前后空格后的字符序列的String对象;
String strTrim= " 开头两个空格和末尾一个空格 ";
System.out.println(strTrim.substring(0).trim()); //开头两个空格和末尾一个空格