字符串
//基本类型和字符串之间转换
//1.基本类型转成字符串
//2.字符串转成基本类型
public class _String类 {
public static void main(String[] args) {
//基本类型包装
int num = 18;
//使用Integer类创建对象
Integer integer = Integer.valueOf(18);
//1.基本类型转成字符串
int n = 208;
//1.1 使用+号
String s = n+"";
System.out.println(s);
//1.2 使用 Integer中的toString()方法
String s1 = Integer.toString(n);
System.out.println(s1);
//1.3 转为X进制的形式
String s2 =Integer.toString(n,16);
//转为16进制,等同于String s3 =Integer.toHexString(n);
//转为8进制Integer.toOctalString(n);
//转为2进制Integer.toBinaryString(n);
//2.字符串转成基本类型
String str="250";
//2.1 使用Integer.parseXXX(),纯数字才能转换;
int n2=Integer.parseInt(str);
//boolean 字符串形式转成基本类型
//"true" --->true 非"true"--->false
String str2 ="true";
boolean b2 = Boolean.parseBoolean(str2);
String str3 ="ture";
boolean b3 = Boolean.parseBoolean(str3);
System.out.println(b2);//true
System.out.println(b3);//false
}
}
常用方法
//1.length();返回字符串长度
//2.charAt(int index);返回某个位置的字符
//3.contains(String str);判断是否包含某个字符
public class _String01 {
public static void main(String[] args) {
String s="dijiaaoteman是最帅的outerman";
System.out.println(s.length());//1.length();返回字符串长度
System.out.println(s.charAt(s.length()-1));//2.charAt(int index);返回某个位置的字符
System.out.println(s.contains("dijia"));//3.contains(String str);判断是否包含某个字符
System.out.println(s.contains("telijia"));//3.contains(String str);判断是否包含某个字符
}
}
//4.toCharArray();将字符串转成数组
//5.indexOf(String str);查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
//6.lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标索引
import java.util.Arrays;
public class _String02 {
public static void main(String[] args) {
String s="迪迦是最帅的迪迦outerman,迪迦牛逼";
System.out.println(Arrays.toString(s.toCharArray()));
System.out.println(s.indexOf("迪迦"));
System.out.println(s.indexOf("迪迦",2));
System.out.println(s.lastIndexOf("迪迦"));
}
}
//7.trim();去掉字符串前后的空格
//8.toUpperCase();把小写转成大写;toLowerCase();把大写转成小写
//9.endWith(str);判断是否以str结尾,startWith(str);判断是否以str开头
public class _String03 {
public static void main(String[] args) {
String s="hello world";
String s1="WTFNI";
System.out.println(s.trim());
System.out.println(s.toUpperCase());
System.out.println(s1.toLowerCase());
String filename="hello.java";
System.out.println(filename.endsWith("a"));
System.out.println(filename.startsWith("he"));
}
}
//10.replace(char old,char new);用新的字符或字符串替换旧的字符或旧的字符串
//11.split();对字符串进行拆分
//补充两个方法 equals 是否相等 、 compare();比较大小
public class _String04 {
public static void main(String[] args) {
String s="迪迦是最帅的迪迦outerman,迪迦牛逼";
System.out.println( s.replace("迪迦","特利迦"));
String say="java is the best programing language,java niubi";
String [] arr =say.split("[ ,]+");//[]表示选择,[ ,]意思是选择空格和逗号都可以,+号表示可以出现多个空格或逗号
// System.out.println(arr.length);
// for (int i = 0; i <arr.length ; i++) {
// System.out.println(arr[i]);
// }
for (String string://同上作用,遍历arr字符串数组
arr) {
System.out.println(string);
}
System.out.println("==================================");
//补充两个方法 equals 是否相等 、 compare();比较大小
String s1="hello";
String s2="HELLO";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写
String s3="abc";//97 98 99
String s4="xyzswe";//120 121 122
System.out.println(s3.compareTo(s4));
String s5="abc";
String s6="abcxyz";
System.out.println(s5.compareTo(s6));//这种情况是比长度
}
}
实例
/*
已知String str="this is a text";
1.将str中的单词单独获取出来
2.将str中的text替换为practice
3.在text前面插入一个easy
4.将每个单词的首字母改为大写
*/
public class _String实例 {
public static void main(String[] args) {
String str="this is a text";
// 1.将str中的单词单独获取出来
String a[] = str.split(" ");
for (String st:
a) {
System.out.println(st+" ");
}
System.out.println("===============================");
//2.将str中的text替换为practice
System.out.println(str.replace("text","practice"));
System.out.println("===============================");
//3.在text前面插入一个easy
System.out.println(str.replace("text","easy text"));
//4.将每个单词的首字母改为大写
System.out.println("===============================");
for (int i = 0; i <a.length ; i++) {
char first = a[i].charAt(0);//先把第一个字母拿出来
char upperfirst=Character.toUpperCase(first);//然后把第一个字母变成大写
String news =upperfirst+a[i].substring(1);//再把第一个字母拼回去
System.out.println(news);
}
}
}
输出结果:
this
is
a
===============================
this is a practice
===============================
this is a easy text
This
Is
A
Text
可变字符串
StringBuffer:可变长字符串,运行效率慢,线程安全
StringBuilder: 可变长字符串,运行效率快,线程不安全
public class _StringBuilder {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
//1.append();追加
sb.append("迪迦");
System.out.println(sb.toString());
sb.append(",我给你光");
System.out.println(sb.toString());
//2.insert();添加
sb.insert(0,"我的");
System.out.println(sb.toString());
//3.replace();替换
sb.replace(0,4,"特利迦");
System.out.println(sb.toString());
//4.reverse();反转
StringBuilder sb1=new StringBuilder();
sb1.append("把手给我");
System.out.println(sb1.reverse());
//5.delete();删除
sb.delete(3,sb.length());
System.out.println(sb.toString());
System.out.println("========================");
//6.清空
sb.delete(0,sb.length());
System.out.println(sb.toString());
}
}
输出结果:
迪迦
迪迦,我给你光
我的迪迦,我给你光
特利迦,我给你光
我给手把
特利迦
========================