单位网站建设方案,焦作建设企业网站公司,h5制作开发价目表,模拟ip访问网站⭐ 作者#xff1a;小胡_不糊涂 #x1f331; 作者主页#xff1a;小胡_不糊涂的个人主页 #x1f4c0; 收录专栏#xff1a;浅谈Java #x1f496; 持续更文#xff0c;关注博主少走弯路#xff0c;谢谢大家支持 #x1f496; String 1. 字符串构造2. String对象的比… ⭐ 作者小胡_不糊涂 作者主页小胡_不糊涂的个人主页 收录专栏浅谈Java 持续更文关注博主少走弯路谢谢大家支持 String 1. 字符串构造2. String对象的比较3. 字符串查找4. 转化5. 字符串替换6. 字符串拆分7. 字符串截取8. 其他操作方法9. 字符串的不可改变性 1. 字符串构造
String类常用的构造方法
public class Main {public static void main(String[] args) {// 使用常量串构造String s1 hello bit;System.out.println(s1);// 直接newString对象String s2 new String(hello bit);System.out.println(s1);// 使用字符数组进行构造char[] array {h,e,l,l,o,b,i,t};String s3 new String(array);System.out.println(s1);}
}注
String是引用类型内部并不存储字符串本身。
例如
public static void main(String[] args) {// s1和s2引用的是不同对象 s1和s3引用的是同一对象String s1 new String(hello);String s2 new String(world);String s3 s1;System.out.println(s1.length()); // 获取字符串长度---输出5System.out.println(s1.isEmpty()); // 如果字符串长度为0返回true否则返回false
}上述代码中的s1、s2、s3在虚拟机栈中三者的存储位置都不同但在堆区中s1和s2所指向的是同一个空间也就是hello的存储空间。
在Java中 “” 引起来的也是String类型对象。
例如
// 打印hello字符串(String对象)的长度
System.out.println(hello.length());2. String对象的比较 字符串的比较是常见操作之一比如字符串排序。 Java中总共提供了4中方式
比较是否引用同一个对象 对于内置类型 比较的是变量中的值对于引用类型 比较的是引用中的地址。
例如
public static void main(String[] args) {int a 10;int b 20;int c 10;// 对于基本类型变量比较两个变量中存储的值是否相同System.out.println(a b); // falseSystem.out.println(a c); // true// 对于引用类型变量比较两个引用变量引用的是否为同一个对象String s1 new String(hello);String s2 new String(hello);String s3 new String(world);String s4 s1;System.out.println(s1 s2); // falseSystem.out.println(s2 s3); // falseSystem.out.println(s1 s4); // true}运行结果 boolean equals(Object anObject) 方法按照字典序比较 字典序字符大小的顺序 String类重写了父类Object中 equals 方法Object中 equals 默认按照 比较String重写 equals 方法后按照如下规则进行比较比如 s1.equals(s2)
public static void main(String[] args) {String s1 new String(hello);String s2 new String(hello);String s3 new String(Hello);// s1、s2、s3引用的是三个不同对象因此比较结果全部为falseSystem.out.println(s1 s2); // falseSystem.out.println(s1 s3); // false// equals比较String对象中的逐个字符// 虽然s1与s2引用的不是同一个对象但是两个对象中放置的内容相同因此输出true// s1与s3引用的不是同一个对象而且两个对象中内容也不同因此输出falseSystem.out.println(s1.equals(s2)); // trueSystem.out.println(s1.equals(s3)); // false}equal的比较方式
public boolean equals(Object anObject) {
// 1. 先检测this 和 anObject 是否为同一个对象比较如果是返回trueif (this anObject) {return true;}
// 2. 检测anObject是否为String类型的对象如果是继续比较否则返回falseif (anObject instanceof String) {
// 将anObject向下转型为String类型对象String anotherString (String) anObject;int n value.length;
// 3. this和anObject两个字符串的长度是否相同是继续比较否则返回falseif (n anotherString.value.length) {char v1[] value;char v2[] anotherString.value;int i 0;
// 4. 按照字典序从前往后逐个字符进行比较while (n-- ! 0) {if (v1[i] ! v2[i])return false;i;}return true;}}return false;}int compareTo(String s) 方法: 按照字典序进行比较 equals返回的是boolean类型而compareTo返回的是int类型。 具体比较方式
先按照字典次序大小比较如果出现不等的字符直接返回这两个字符的大小差值如果前k个字符相等(k为两个字符长度最小值)返回值两个字符串长度差值
例如
public static void main(String[] args) {String s1 new String(abc);String s2 new String(ac);String s3 new String(abc);String s4 new String(abcdef);System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1System.out.println(s1.compareTo(s3)); // 相同输出 0System.out.println(s1.compareTo(s4)); // 前k个字符完全相同输出长度差值 -3}int compareToIgnoreCase(String str) 方法
与compareTo方式相同但是忽略大小写比较例如
public static void main(String[] args) {String s1 new String(abc);String s2 new String(ac);String s3 new String(ABc);String s4 new String(abcdef);System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同输出长度差值 -3}3. 字符串查找
常用查找的方法
方法功能char charAt(int index)返回index位置上字符如果index为负数或者越界抛出IndexOutOfBoundsException异常int indexOf(int ch)返回ch第一次出现的位置没有返回-1int indexOf(int ch, int fromIndex)从fromIndex位置开始找ch第一次出现的位置没有返回-1int indexOf(String str)返回str第一次出现的位置没有返回-1int indexOf(String str, int fromIndex)从fromIndex位置开始找str第一次出现的位置没有返回-1int lastIndexOf(int ch)从后往前找返回ch第一次出现的位置没有返回-1int lastIndexOf(int ch, int fromlndex)从fromIndex位置开始找从后往前找ch第一次出现的位置没有返回-1int lastIndexOf(Stringstr)从后往前找返回str第一次出现的位置没有返回-1int lastIndexOf(String str, int fromlndex)从fromIndex位置开始找从后往前找str第一次出现的位置没有返回-1
实例
public static void main(String[] args) {String s aaabbbcccaaabbbccc;System.out.println(s.charAt(3)); // bSystem.out.println(s.indexOf(c)); // 6System.out.println(s.indexOf(c, 10)); // 15System.out.println(s.indexOf(bbb)); // 3System.out.println(s.indexOf(bbb, 10)); // 12System.out.println(s.lastIndexOf(c)); // 17System.out.println(s.lastIndexOf(c, 10)); // 8System.out.println(s.lastIndexOf(bbb)); // 12System.out.println(s.lastIndexOf(bbb, 10)); // 3}4. 转化
数值和字符串转化
public static void main(String[] args) {// 数字转字符串String s1 String.valueOf(1234);String s2 String.valueOf(12.34);String s3 String.valueOf(true);System.out.println(s1);//1234System.out.println(s2);//12.34System.out.println(s3);//trueSystem.out.println();// 字符串转数字// Integer、Double等是Java中的包装类型int data1 Integer.parseInt(1234);double data2 Double.parseDouble(12.34);System.out.println(data1);//1234System.out.println(data2);//12.34}大小写转换
public static void main(String[] args) {String s1 heLlo;String s2 HELLO;// 小写转大写System.out.println(s1.toUpperCase());//HELLO// 大写转小写System.out.println(s2.toLowerCase());//hello}字符串转数组
public static void main(String[] args) {String s hello;// 字符串转数组char[] ch s.toCharArray();for (int i 0; i ch.length; i) {System.out.print(ch[i]);}System.out.println();// 数组转字符串String s2 new String(ch);System.out.println(s2);}格式化
//格式化public static void main(String[] args) {String s String.format(%d-%d-%d, 2019, 9,14);System.out.println(s);//2019-9-14}5. 字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据可用的方法如下
方法功能String replaceAll(String regex, String replacement)替换所有的指定内容String replaceFirst(String regex, String replacement)替换首个内容
实例
public static void main(String[] args) {String str helloworld ;System.out.println(str.replaceAll(l, _));//he__owor_dSystem.out.println(str.replaceFirst(l, _));//he_loworld}字符串是不可变对象替换-不修改当前字符串而是产生一个新的字符串。
6. 字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。可用方法如下
方法功能String[] split(String regex)将字符串全部拆分String[] split(String regex, int limit)将字符串以指定的格式拆分为limit组
实例实现字符串的拆分处理
public static void main(String[] args) {String str hello world ;String[] result str.split( ) ; // 按照空格拆分for(String s: result) {System.out.println(s);}}运行结果 实例字符串的部分拆分
public static void main(String[] args) {String str hello beautiful world ;String[] result str.split( ,2) ;for(String s: result) {System.out.println(s);}}运行结果 有些特殊字符作为分割符可能无法正确切分需要加上转义。
实例拆分IP地址
public static void main(String[] args) {String str 192.168.1.1 ;String[] result str.split(\\.) ;for(String s: result) {System.out.println(s);}}运行结果 注
字符|“”*“”都得加上转义字符前面加上 “\” 。而如果是 “” 那么就得写成 “\\” 。如果一个字符串中有多个分隔符可以用|作为连字符。
实例多次拆分
public static void main(String[] args) {String str namezhangsanage18 ;String[] result str.split() ;for (int i 0; i result.length; i) {String[] temp result[i].split() ;System.out.println(temp[0] temp[1]);}}运行结果 7. 字符串截取
从一个完整的字符串之中截取出部分内容。可用方法如下
方法功能String substring(int beginIndex)从指定索引截取到结尾String substring(int beginIndex, int endIndex)截取部分内容
实例
public static void main(String[] args) {String str helloworld ;System.out.println(str.substring(5));System.out.println(str.substring(0, 5));}运行结果
注
索引从0开始。注意前闭后开区间的写法substring(0,5) 表示包含 0 号下标的字符不包含 5 号下标。
8. 其他操作方法
方法功能String trim()去掉字符串中的左右空格,保留中间空格String toUpperCase()字符串转大写String toLowerCase()字符串转小写
实例trim()方法的使用
public static void main(String[] args) {String str hello world ;System.out.println([str]);System.out.println([str.trim()]);}运行结果 trim 会去掉字符串开头和结尾的空白字符空格、换行、制表符等)。
实例大小写转换
public static void main(String[] args) {String str hello%$$%#$%world 哈哈 ;System.out.println(str.toUpperCase());System.out.println(str.toLowerCase());}运行结果 9. 字符串的不可改变性
String是一种不可变对象. 字符串中的内容是不可改变。字符串不可被修改是因为
String类在设计时就是不可改变的它的内容在创建好之后就不能被修改。 String类中的字符实际保存在内部维护的value字符数组中 String类被final修饰表明该类不能被继承 value被修饰被final修饰表明value自身的值不能改变即不能引用其它字符数组但是其引用空间中的内容可以修改。 所有涉及到可能修改字符串内容的操作都是创建一个新对象改变的是新对象 final修饰类表明该类不想被继承final修饰引用类型表明该引用变量不能引用其他对象但是其引用对象中的内 容是可以修改的。 为什么String要设计成不可变的
方便实现字符串对象池。如果 String 可变那么对象池就需要考虑写时拷贝的问题了。不可变对象是线程安全的。不可变对象更方便缓存 hash code作为 key 时可以更高效的保存到 HashMap 中。