String
1. String引入
1.1 构造方法
public static void main1(String[] args) {//构造方法String s1 = "hello world";String s2 = new String("yuanwei");char[] values ={'a','b','c'};String s3= new String(values);System.out.println(s1);System.out.println(s2);System.out.println(s3);}
1.2 常见函数调用
public static void main2(String[] args) {//常见函数调用String s1 = "hello world";System.out.println(s1.length());//长度,调用的是函数int[]array ={1,2,3,4,5};System.out.println(array.length);//length是属性String s2="";System.out.println(s2.isEmpty());//s2这个引用指向的对象是空String s3=null;//s3不指向任何对象System.out.println("yuanwei".length());//""引起来的常量也是String类,常量也可直接调用函数}
1.3 字符串比较
public static void main3(String[] args) {String s1="hello";String s2="yuanwei";//比较两个引用指向对象内容是否一致.equals是String重写了Object的接口System.out.println(s1.equals(s2));//System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写System.out.println(s1.compareTo(s2));//if s1>s2,返回正数,反之负数,相等返回0System.out.println(s1.compareToIgnoreCase(s2));//忽略大小写比较字典序}
1.4 字符串查找
public static void main4(String[] args) {//字符串查找String s1="hello";//1. charAt()System.out.println(s1.charAt(2));for (int i = 0; i < s1.length() ;i++) {System.out.print(s1.charAt(i));}System.out.println();//2. indexOfint index1 = s1.indexOf('l');//返回第一次出现的位置System.out.println(index1);String s2= "abcdabcdabcd";int index2 = s2.indexOf("da");//主串中查找子串位置 KMP算法System.out.println(index2);//3. lastIndexOf() 从后往前找int index3= s2.lastIndexOf("abc");System.out.println(index3);int index4= s2.lastIndexOf("acbd",9);//end=fromIndex开始往前找}
1.5 字符串转换
public static void main5(String[] args) {//字符串转换String s1=String.valueOf(123);//int->StringString s2 =String .valueOf('c');//char->StringSystem.out.println(s1);System.out.println(s2);String s3= String.valueOf(new Person());//Object->StringSystem.out.println(s3);//调用的是Object的toString方法System.out.println("===================");String s4="123";int data=Integer.parseInt(s4);//类名调用的都是静态方法System.out.println(s4);//基本类型(int) 对应的 类类型(Integer)double data2=Double.parseDouble("123.34");}public static void main6(String[] args) {//大小写转化String s1 ="abcd";String ps1=s1.toUpperCase();System.out.println(ps1);String s2="AFSfdas";System.out.println(s2.toLowerCase());}
1.6 字符串替换
public static void main(String[] args) {//并不修改原来的值,返回值都是新生成一个对象String s1="abcdabcdeacbde";String ret1=s1.replace('a', 'y');System.out.println(ret1);System.out.println("===============");String ret2=s1.replace("abc","000");System.out.println(ret2);System.out.println("===============");String ret3=s1.replaceAll("abc","000");System.out.println(ret3);System.out.println("===============");String ret4=s1.replaceFirst("cb","yuan");System.out.println(ret4);}
1.7 字符串转数组
public static void main7(String[] args) {//字符串转数组String s1 ="abcd";char[] array = s1.toCharArray();for(int i=0;i<array.length;i++){System.out.println(array[i]);}//格式化输出String s=String.format("%d-%d-%d",2001,6,20);System.out.println(s);}
1.8 字符串拆分
public static void main(String[] args) {//涉及到多次拆分的情况String str1="name=yuanwei&age=22&sex=male";String[] strs=str1.split("&");for(int i=0;i<strs.length;i++){System.out.println(strs[i]);}//name=yuanwei//age=22//sex=malefor(int i=0;i<strs.length;i++){
// System.out.println(strs[i]);String[] strings=strs[i].split("=");for(String s:strings){System.out.println(s);}}//name//yuanwei//age//22//sex//male}public static void main9(String[] args) {//字符串拆分String s1= "hello yuanwei hello ahahah";String[] array1 = s1.split(" ");//参数是Stringfor (int i = 0; i < array1.length; i++) {System.out.println(array1[i]);}//hello//yuanwei//hello//ahahahSystem.out.println("============");String[] array2 = s1.split(" ",2);//最多分两组for (int i = 0; i < array2.length; i++) {System.out.println(array2[i]);}//hello//yuanwei hello ahahahString s3="192.168.16.20";String[] ret =s3.split("\\.");//特殊字符. | * + 需要进行转义,加上两个\\for (int i = 0; i < ret.length; i++) {System.out.println(ret[i]);}//192//168//16//20String s4="192\\168\\16\\20";String[] ret2 =s4.split("\\\\");for (int i = 0; i < ret2.length; i++) {System.out.println(ret2[i]);}//192//168//16//20String s5="192&16816=20";String[] ret3 =s4.split("&|=");//多个分隔符for (int i = 0; i < ret3.length; i++) {System.out.println(ret3[i]);}}
1.9 字符串截取
public static void main16(String[] args) {//字符串截取,也是返还一个新的对象String str="abcdef";String s1= str.substring(2);//[beginIndex...]System.out.println(s1);String s2=str.substring(2,3);//[from,end) 注意所有区间都是左闭右开System.out.println(s2);//cdef//c}public static void main(String[] args) {//删除左右两边的空格String str =" abcd hello ";String s1=str.trim();System.out.println(s1);//abcd hello}
2. 字符串不可变性
只要是""引起来的对象,都放在常量值区中,是不可改变的,都会产生新的对象返回.
逻辑上JVM内存划分为java虚拟机栈,本地方法栈,堆,方法区,程序计数器 5个部分,方法区就是堆的一部分.
3. StringBuffer StringBuilder
字符串拼接
+拼接会产生很多的临时对象,导致效率会非常的低.
public static void main(String[] args) {String str="hello";StringBuffer stringBuffer =new StringBuffer();//新stringBuffer对象stringBuffer.append(str);stringBuffer.append("yuanwei");str改变引用指向,指向了一个新产生的对象StringBuilder完成拼接,得到helloyuanwei并使用toString()new的新对象str= stringBuffer.toString();System.out.println(str);//helloyuanwei}public static void main20(String[] args) {String str="hello";//对象1str+="yuanwei";//对象2System.out.println(str);//对象3->StringBuilder,toString又创建一个String对象,赋值给str}
例子演示:
public class String_builder {public static void main(String[] args) {//在同一个对象上进行修改.\//将常量值取出来之后构建一个对象,然后无论怎么操作,都只会在这个对象上操作StringBuilder stringBuilder=new StringBuilder("abc");System.out.println(stringBuilder);stringBuilder.reverse();System.out.println(stringBuilder);//abc//cbaStringBuilder stringBuilder2=new StringBuilder("abc");System.out.println(stringBuilder2);stringBuilder2.append(10);stringBuilder2.append(10);stringBuilder2.append(10);System.out.println(stringBuilder2);//abc101010 调用的是stringBuilder2.toString()}
}
public class string {public boolean isValid(char ch){if((ch>='0' && ch<='9') || (ch>='a' && ch<='z')){return true;}return false;}public boolean isPalindStr(String str){//回文字符串判断str.toLowerCase();//忽略大小写char[] array = str.toCharArray();int left=0,right=array.length-1;while (left<right){//循环走一定注意双指针相对位置关系while(left<right && !isValid(array[left])){left++;}while(left<right && !isValid(array[right])){right--;}if(array[left] != array[right]){return false;}else{left++;right--;}}return true;}//处理连续输入问题public static void main(String[] args) {Scanner scanner=new Scanner(System.in);while(scanner.hasNextLine()){//如果有下一行数据,trueString str = scanner.nextLine();//读取一行//do...int index =str.lastIndexOf(" ");String ret = str.substring(index+1);System.out.println(ret);}}
}