sring类

String: value[] 底层调用Arrays.copyOf (public final class String) 是不可变类,不能修改原来的值,只能产生新对象 单线程

String s = new String(“hello”)和String s = “hello”;的区别?
有。前者会创建2个对象,后者创建1个对象。
==:比较引用类型比较的是地址值是否相同
equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。

public class Demo2{
	public static void main(String[] args) {
		String s1 = new String("hello");
		String s2 = "hello";
 
		System.out.println(s1 == s2);// false
		System.out.println(s1.equals(s2));// true
	}
}

在这里插入图片描述

字符串如果是变量相加,先开空间,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建

public class Demo3 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);// false
		System.out.println(s3.equals(s1 + s2));// true
 
		System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
		System.out.println(s3.equals("hello" + "world"));// true
	}
}

int length():获取字符串的长度。
char charAt(int index):获取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次 String substring(int start):从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
**

public class Demo4 {
	public static void main(String[] args) {
		// 定义一个字符串对象
		String s = "helloworld";
 // int length():获取字符串的长度。
        System.out.println("s.length:" + s.length());
		System.out.println("----------------------");
 // char charAt(int index):获取指定索引位置的字符
		System.out.println("charAt:" + s.charAt(7));
		System.out.println("----------------------");
 // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
		System.out.println("indexOf:" + s.indexOf('l'));
		System.out.println("----------------------");
 // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
		System.out.println("indexOf:" + s.indexOf("owo"));
		System.out.println("----------------------");
 // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
		System.out.println("indexOf:" + s.indexOf('l', 4));
		System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
		System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
		System.out.println("----------------------");
 // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start
		System.out.println("substring:" + s.substring(5));
		System.out.println("substring:" + s.substring(0));
		System.out.println("----------------------");
 // String substring(int start,intend):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
		System.out.println("substring:" + s.substring(3, 8));
		System.out.println("substring:" + s.substring(0, s.length()));
	}
}

StringBuffer.append 追加 不产生新对象 ; 多线程 synchronized线程保护
StringBuilder append 不产生新对象 单线程

public class StringDemo {

    public static void main(String[] args) {
        String str1 = "abcde";
        String str2 = "hello";
        String str3 = str1+str2+"world";
        System.out.println(str3);
    }

    public static void main4(String[] args) {
        String str1 = "abcde";
        String str2 = "abcde";
        System.out.println(Integer.toHexString(str1.hashCode()));
        str1 = str1+"fg";
        System.out.println(Integer.toHexString(str1.hashCode()));
        System.out.println("===========================");


        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer);
        System.out.println(Integer.toHexString(stringBuffer.hashCode()));
        stringBuffer.append("world");
        System.out.println(stringBuffer);
        System.out.println(Integer.toHexString(stringBuffer.hashCode()));
        System.out.println("=============stringBuilder==============");
        StringBuilder stringBuilder = new StringBuilder("tulun");//"tulunworld"
        System.out.println(stringBuilder);
        System.out.println(Integer.toHexString(stringBuilder.hashCode()));
        stringBuilder.append("world");
        System.out.println(stringBuilder);
        System.out.println(Integer.toHexString(stringBuilder.hashCode()));

    }

练习题:逆置数组
"abcdef" ===> "cdefab"
.整体逆置fedcba
here is tulun==>tulun is here

 * 1、abcdef
 */

public class HomeWork {

    public static String reverse1(String str,int begin,int end) {
        char[] ch = str.toCharArray();
        char tmp;
        while(begin < end) {
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin++;
            end--;
        }
        return String.copyValueOf(ch);//将ch转化为字符串
    }
    public static void reverse(char[] ch,int begin,int end) {
        //char[] ch = str.toCharArray();
        char tmp;
        while(begin < end) {
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin++;
            end--;
        }
        //return String.copyValueOf(ch);//将ch转化为字符串
    }
    //here is tulun ====> 微信部门
    public static String reverseSentence(String str) {
        if(str == null) {
            return null;
        }
        char[] ch = str.toCharArray();
        //str = reverse(str,0,str.length()-1);//""
        reverse(ch,0,ch.length-1);//
        int i = 0;//单词的开始
        int j = 0;//单词的结束
        while(i < str.length()) {
            if(ch[i] == ' ') {//str.chaAt(i)
                i++;
                j++;
            } else if(j == ch.length || ch[j] == ' ') {
                reverse(ch,i,--j);
                i = ++j;
            } else {
                j++;
            }
        }
       return String.copyValueOf(ch);
    }

    public static void leftRoadString(String str,int n) {//2
        if(str == null || n < 0 || n > str.length()) {
            return;
        }
        //abcdef
        int left = 0;
        int leftend = n-1;
        int right = n;
        int rightend = str.length()-1;
        str = reverse1(str,left,leftend);//bacdef
        str = reverse1(str,right,rightend);//bafedc
        str = reverse1(str,left,rightend);//cdefab
        System.out.println(str);
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(reverseSentence(str));
       /* int n = scanner.nextInt();
        leftRoadString(str,n);*/
        /*String string = "abcdef";
        char[] chars = new char[10];
        //string.getChars(chars,0);
        string.getChars(0,4,chars,0);*/
    }
}

猜你喜欢

转载自blog.csdn.net/lmx9813/article/details/83388015