Java String类常用类型
Fly upward 人气:01.创建字符串的方法
1.1构造
方式一、直接构造
String str = "fly";
方式二 、调用构造方法进行构造对象
String str1 = new String("name");
方式三、利用字符数组进行转换
char[] ch = {'a','s','d'}; String str2 = new String(ch);
1.2引用对象
public static void main(String[] args) { //str 、 str1 都指向同一个对象 String str = "asd"; String str1 = str; System.out.println(str); System.out.println(str1); System.out.println("---------------"); str = "fly";//str指向新的对象,str1 没有变,还指着原来的对象 System.out.println(str); System.out.println(str1); }
public static void func(String s,char[] array) { s = "fly"; array[0] = 'm'; } public static void main(String[] args) { String str = "asfgh"; char[] ch = {'w','s','d'}; func(str,ch); System.out.println(str); System.out.println(Arrays.toString(ch)); }
2.字符串的比较
public static void main(String[] args) { String str1 = "hello"; String str2 = "hello"; System.out.println(str1 == str2); System.out.println(str1.equals(str2)); }
public static void main(String[] args) { String str1 = "ABC"; String str2 = "abc"; boolean flg = str1.equalsIgnoreCase(str2);//不看大小写判断字符串是否相等 System.out.println(flg); }
3.字符串的不可改变性
字符串是一种不可变对象 . 它的内容不可改变
public static void main(String[] args) { String str = "hello "; str = str + "world "; str += "!!!"; System.out.println(str); }
4.数组转字符串
public static void main(String[] args) { char[] val = {'a','b','c','d','f'}; String str = new String(val); String str2 = new String(val,1,3);//区间变字符串 System.out.println(str); System.out.println("------------"); String str3 = "hello"; char ch = str3.charAt(2);//获取下标字符 System.out.println(ch); char[] chars = str3.toCharArray();//把str3指向的字符串变为数组 System.out.println(Arrays.toString(chars)); }
5.判断是否是数字字符串 isNumberChar( )
public static boolean isNumberChar(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i);//逐个获取字符 boolean flg = Character.isDigit(c);//判断某个字符是不是数字 if(flg == false) { return false; } /*if (c < '0' || c > '9') { return false; }*/ } return true; } public static void main(String[] args) { String str = "0123456789"; System.out.println(isNumberChar(str)); }
6.字节变字符串
public static void main(String[] args) { byte[] bytes = {97,98,99,100};//字节对应的字母 String str = new String(bytes); System.out.println(str); String str2 = new String(bytes,1,3); System.out.println(str2); System.out.println("-------------"); String str3 = "flying"; byte[] bytes1 = str3.getBytes();//获取字符对应的字节 System.out.println(Arrays.toString(bytes1));//以数组形式输出 }
7.字符串的查找
public static void main(String[] args) { String str = "babcdabcab"; String str2 = "abc"; boolean flg = str.contains(str2);//判断是否含有子字符串 System.out.println(flg); System.out.println("-----------"); int index = str.indexOf(str2);//找出子字符串第一次开始出现的位置 System.out.println(index); System.out.println(str.lastIndexOf(str2,8));//从指定位置往前找 System.out.println(str.startsWith("a"));//判断是否是以其字母开头的字符串 System.out.println(str.startsWith("a",1));//判断某位置是否是该字母 System.out.println(str.endsWith("ab"));//判断是否是以该字符串结尾 }
8.字符的替换
public static void main(String[] args) { String str = "babcdabcab"; String ret = str.replace('a','f');//将所有字符a替换成f //String ret = str.replaceFirst('a','f');//将第一次出现的字符a替换成f System.out.println(ret); }
9.字符串的分割
9.1以单个符号来分割
public static void main(String[] args) { String str = "name=fly&age=20"; String[] strings = str.split("&");//以字符&分割 for (String s:strings) { // System.out.println(s); String[] ss = s.split("=");//分割之后再以=分割 for (String tmp:ss) { System.out.println(tmp); } } }
9.2多个分隔符分割
public static void main(String[] args) { String str3 = "fly%12&age#hello"; String[] ret = str3.split("%|&|#");//多个分隔符分割,用|隔开 for (String sss:ret) { System.out.println(sss); } }
9.3特殊符号分割
1. 字符 "|","*","+" 都得加上转义字符,前面加上 "\".
2. 而如果是 "" ,那么就得写成 "\\".
3. 如果一个字符串中有多个分隔符,可以用 "|" 作为连字符 .
public static void main(String[] args) { String str = "191.157.1.1"; String[] result = str.split("\\."); for (String s:result) { System.out.println(s); } System.out.println("-------------"); String str2 = "191\\157\\1\\1"; String[] strings = str2.split("\\\\",3);//4是分为四组 for (String ss:strings) { System.out.println(ss); } String str3 = "fly%12&age#hello"; String[] ret = str3.split("%|&|#");//多个分隔符分割,用|隔开 for (String sss:ret) { System.out.println(sss); } }
10.提取子串str.substring( )
public static void main(String[] args) { String str = "abcdefg"; String sub = str.substring(2,4);//提取子串,左闭右开 System.out.println(sub); }
11.去空格
public static void main(String[] args) { String str = " abc def "; String ret = str.trim();//去除字符串左右两边的空格 System.out.print(ret); System.out.println("---"); }
12.字符串的拼接
public static void main(String[] args) { String str = "abc"; String ret = str.concat("fly");//字符串的拼接 System.out.println(ret); }
13.StringBuffer
13.1字符串的拼接
public static void main(String[] args) { String str = "hello"; StringBuffer sb = new StringBuffer(); sb.append(str); for (int i = 0; i < 10; i++) { sb.append(i); str = sb.toString(); } System.out.println(str); }
13.2方法的返回类型
public static StringBuffer func() { String str = "fly"; return new StringBuffer(str); } public static void main(String[] args) { System.out.println(func()); }
总结
加载全部内容