String 是只读对象。如果需要对字符串做修改,应该使用 StringBuffer & StringBuilder 类。
String 直接创建的字符串存储在公共池?中,而 new 创建的字符串对象在堆?上。
String s1 = "TEST"; // String 直接创建
String s2 = s1; // 相同引用,指向一个对象而不是创建一个对象
String s3 = new String("TEST"); // String 对象创建,构造函数创建
通过字符数组创建字符串
char[] helloArray = { 'T','E','S','T'};
String helloString = new String(helloArray);
字符串的比较
String a = "Hello World";//存到了字符串池的某个地址上
String b = "Hello World";//字符串池中有了,直接复用这个地址
String c = new String("Hello World");//拿一个新的地址空间来存储
System.out.println(a==b); //地址一致
System.out.println(a.equals(b)); //内容一致
System.out.println(a==c); //地址不一致
System.out.println(a.equals(c)); //内容一致
/*比较地址意义不大,所以几乎不用。一般用equals来比较内容上是否一致。*/
String 访问器 方法(获取有关对象的信息的方法称为访问器方法)
字符串长度
int len = helloString.length();
System.out.println( "helloString长度 : " + len );
.concat 连接字符串
String coc= new String("haha");
String con= new String("abc"+"ded"+coc);//直接连接
字符位置
String strings="abcdefgcda";
System.out.println(strings.charAt(0));//查询某位置的字符
System.out.println(strings.indexOf('b'));//查询某字符的位置
System.out.println(strings.indexOf("cda"));//查询某字符串的首字符位置
.format 格式化字符串
格式化有关内容的符号如下图所示:
%s | 字符串 | %x | 十六进制整 | %c | 指数形式 | %n | 换行符 |
%c | 字符 | %o | 八进制整 | %g | 通用浮点 | %t? | 日期时间 |
%b | 逻辑 | %f | 十进制浮点 | %h | 散列码形式 | ||
%d | 十进制整 | %a | 十六进浮 | %% | 百分比形式 |
float a=1.0F;
int b=2;
String c="hello";
System.out.printf("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
"is %s",a,b,c);
String fs;
fs = String.format("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
" %s",a,b,c);
System.out.println(fs);
打印格式化日期时间
tF | 年-月-日 | tr | 时:分:秒 上午(12H) |
tD | 月/日/年 | tT | 时分秒 (24H) |
tR | 时:分(24H) |
Date today=new Date();
String a = String.format("%tF", today);
System.out.println(a);
DateFormat格式化日期时间
Date date = new Date(); //获取系统时间
DateFormat df = null; //
df = DateFormat.getDateInstance(); //
System.out.println("日期:"+df.format(date)); //日期:2021年9月9日
SimpleDateFormat格式化日期时间
符号 | 描述 | 符号 | 描述 |
---|---|---|---|
a | 上/下午 | D | 某天(1-366) |
d | 某天(1-31) | E | 星期X |
h | 某时(12) | F | 某月工作日数 |
k | 某时(24) | G | 纪元(AD/BC) |
m | 某分(1-59) | H | 某小时(1-23) |
s | 某秒(1-59) | K | 上午或下午某时(1-11) |
w | 某星期(1-52) | M | 月份 |
y | 年 | S | 秒钟之毫秒 |
z | 时区 | W | 月之星期数(1-5) |
Date dates = new Date();
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat("yyyy/MMM/dd hh:mm:ss a");
System.out.println(sdf.format(dates));
sdf = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sdf.format(dates));
sdf = new SimpleDateFormat("E yy-MM-dd h:mm:ss z");
System.out.println(sdf.format(dates));
/*
2021/9月/09 06:59:17 下午
2021年09月09日
周四 21-09-09 6:59:17 CST
*/
NumberFormat数字格式化
NumberFormat nf = null;
nf = NumberFormat.getNumberInstance();
System.out.println(nf.format(123456.789));//一般
nf = NumberFormat.getNumberInstance(Locale.FRENCH);
System.out.println(nf.format(123456.789));//法国数字表示
nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(123456.789));//货币显示
nf = NumberFormat.getPercentInstance();
System.out.println(nf.format(0.25));//百分数显示
更多string方法: Java String 类 | 菜鸟教程
String Buffer 字符串缓冲器
StringBuffer test=new StringBuffer(""); //实例化字符串缓存对象
test.append('a'+","); //使用添加方法
test.append('b'); //使用添加方法
System.out.println(test); //打印测试
System.out.println(test.toString()); //转化为字符串打印测试
注意:在append连接多个字符时,应至少使用一对双引号,如果仅使用单引号将作为数字相加。
注意:toString方法并不会改变他的buffer性质,只是对本次调用转换,一般调用不必转为字符串。