怎么去做网站,在百度怎样建网站,有没有如何做网站的书,wordpress 外观 自定义1、字符串与字符数组的转换字符串可以使用toCharArray()方法变成一个字符数组#xff0c;也可以使用String类的构造方法把一个字符数组变成一个字符串。public class StringAPIDemo01{public static void main(String[] args){String str1 hello; //定义字符串ch…1、字符串与字符数组的转换字符串可以使用toCharArray()方法变成一个字符数组也可以使用String类的构造方法把一个字符数组变成一个字符串。public class StringAPIDemo01{public static void main(String[] args){String str1 hello; //定义字符串char c[] str1.toCharArray(); //将字符串变为字符数组for(int i0;iSystem.out.println(c[i] \t);}System.out.println();String str2 new String(c); //将全部字符数组变为StringString str3 new String(c,0,3); //将部分字符数组变为StringSystem.out.println(str2);System.out.println(str23);}}程序运行结果h e l l ohellohel2、从字符串中取出指定位置的字符直接使用String类中的charAt()方法取出字符串指定位置的字符例如。public class StringAPIDemo02{public static void main(String[] args){String str1 hello;System.out.println(str1.charAt(3)); //取出字符串中第4个字符}}程序运行结果l3、把一个字符串变成一个byte数组也可以把一个byte数组变成一个字符串。字符串可以通过getBytes()方法将String变为一个byte数组然后可以通过String的构造方法将一个字节数组重新变为字符串例如public class StringAPIDemo03{public static void main(String[] args){String str1 hello;byte b[] str1.getBytes(); //将字符串变为byte数组System.out.println(new String(b)); //将全部byte数组变为字符串System.out.println(new String(b1,3)); //将部分byte数组变为字符串}}程序运行结果helloell4、取得一个字符串的长度在String中使用length()方法取得字符串的长度例如public class StringAPIDemo04{public static void main(String[] args){String str1 hello chenjunlin;byte b[] str1.getBytes(); //定义字符串变量System.out.println(\ str1 \t 的长度为 str1.length());}}程序运行结果hello chenjunlin 的长度为:15注length与length()区别在数组操作中使用length取得数组的长度但是操作的最后没有()而字符串调用length是一个方法只要是方法后面都有“()”。5、查找一个指定的字符串是否存在在String中使用indexOf()方法可以返回指定的字符串位置如果不存在则返回-1例如public class StringAPIDemo05{public static void main(String[] args){String str1 chenjunlin;System.out.println(str1.indexOf(c)); //查找返回位置System.out.println(str1.indexOf(c,3)); //查到返回位置从第4个开始查找System.out.println(str1.indexOf(x)); //没有查到返回-1}}6、去掉左右空格在开发过程中用户输入的数据中可能含有大量的空格使用trim()方法可以去掉字符串左右空格例如public class StringAPIDemo06{public static void main(String[] args){String str1 chenjunlin ;System.out.println(str1.trim()); //查找返回位置//System.out.println(str1.indexOf(c,3)); //查到返回位置从第4个开始查找//System.out.println(str1.indexOf(x)); //没有查到返回-1}}7、字符串截取在String中提供了两个substring()方法一个是从指定位置截取到字符串结尾另一个是截取指定范围内的内容例如public class StringAPIDemo07{public static void main(String[] args){String str1 hello world;System.out.println(str1.substring(6)); //从第7个位置开始截取System.out.println(str1.substring(0,5)); //截取0~5个位置的内容}}程序运行结果worldhello8、按照指定的字符串拆分字符串在String中通过split()方法可以进行字符串的拆分操作拆分的数据将以字符串数组的形式返回例如public class StringAPIDemo08{public static void main(String[] args){String str1 hello world; //将空格进行字符串的拆分String s[] str1.split( );for(int i0;iSystem.out.println(s[i]);}}}程序运行结果helloworld9、字符串的大小写转换在用户输入信息是有时需要统一输入数据的大小写此时使用toUpperCase()和toLowerCase()两个方法完成大小写的转换操作例如10、判断是否以指定的字符串开头和结尾在String中使用startsWith()方法可以判断字符串是否以指定的内容开头使用endsWith()方法可以判断字符串是否以指定的内容结尾例如11、不区分大小写进行字符串比较在String中可以通过equals()方法进行字符串内容的比较但这种比较方法是区分大小写的比较如果要完成不区分大小写的比较可以使用equalsIgnoreCase()方法例如12、将一个指定的字符串替换成其他的字符串使用string的replaceAll()方法可以将字符串的指定内容进行替换例如