数字转字符串
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//方法1
String str = String.valueOf(i);
//方法2
Integer it = i;
String str2 = it.toString();
}
}
字符串转数字
public class TestNumber {
public static void main(String[] args) {
String str = "999";
int i= Integer.parseInt(str);
System.out.println(i);
}
}
数学中的一些函数
public class TestNumber {
public static void main(String[] args) {
float f1 = 5.4f;
float f2 = 5.5f;
//5.4四舍五入即5
System.out.println(Math.round(f1));
//5.5四舍五入即6
System.out.println(Math.round(f2));
//得到一个0-1之间的随机浮点数(取不到1)
System.out.println(Math.random());
//得到一个0-10之间的随机整数 (取不到10)
System.out.println((int)( Math.random()*10));
//开方
System.out.println(Math.sqrt(9));
//次方(2的4次方)
System.out.println(Math.pow(2,4));
//π
System.out.println(Math.PI);
//自然常数e=2.7.......
System.out.println(Math.E);
}
}
格式化输出
public class TestNumber {
public static void main(String[] args) {
String name ="盖伦";
int kill = 8;
String title="超神";
//直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
String sentence = name+ " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号";
System.out.println(sentence);
//使用格式化输出
//%s表示字符串,%d表示数字,%n表示换行
String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
System.out.printf(sentenceFormat,name,kill,title);
}
}
printf 和format
public class TestNumber {
public static void main(String[] args) {
String name ="盖伦";
int kill = 8;
String title="超神";
String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
//使用printf格式化输出
System.out.printf(sentenceFormat,name,kill,title);
//使用format格式化输出
System.out.format(sentenceFormat,name,kill,title);
}
}
换行符
换行符就是另起一行 — ‘\n’ 换行(newline)
回车符就是回到一行的开头 — ‘\r’ 回车(return)
在eclipse里敲一个回车,实际上是回车换行符
Java是跨平台的编程语言,同样的代码,可以在不同的平台使用,比如Windows,Linux,Mac
然而在不同的操作系统,换行符是不一样的
(1)在DOS和Windows中,每行结尾是 “\r\n”;
(2)Linux系统里,每行结尾只有 “\n”;
(3)Mac系统里,每行结尾是只有 “\r”。
为了使得同一个java程序的换行符在所有的操作系统中都有一样的表现,使用%n,就可以做到平台无关的换行
public class TestNumber {
public static void main(String[] args) {
System.out.printf("这是换行符%n");
System.out.printf("这是换行符%n");
}
}
其他常用格式化符号
import java.util.Locale;
public class TestNumber {
public static void main(String[] args) {
int year = 2020;
//总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
//直接打印数字
System.out.format("%d%n",year);
//总长度是8,默认右对齐
System.out.format("%8d%n",year);
//总长度是8,左对齐
System.out.format("%-8d%n",year);
//总长度是8,不够补0
System.out.format("%08d%n",year);
//千位分隔符
System.out.format("%,8d%n",year*10000);
//小数点位数
System.out.format("%.2f%n",Math.PI);
//不同国家的千位分隔符
System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
}
}
char的常用方法
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println(Character.isLetter('a'));//判断是否为字母
System.out.println(Character.isDigit('a')); //判断是否为数字
System.out.println(Character.isWhitespace(' ')); //是否是空白
System.out.println(Character.isUpperCase('a')); //是否是大写
System.out.println(Character.isLowerCase('a')); //是否是小写
System.out.println(Character.toUpperCase('a')); //转换为大写
System.out.println(Character.toLowerCase('A')); //转换为小写
String a = 'a'; //不能够直接把一个字符转换成字符串
String a2 = Character.toString('a'); //转换为字符串
}
}
常见的转义字符
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println("使用空格无法达到对齐的效果");
System.out.println("abc def");
System.out.println("ab def");
System.out.println("a def");
System.out.println("使用\\t制表符可以达到对齐的效果");
System.out.println("abc\tdef");
System.out.println("ab\tdef");
System.out.println("a\tdef");
System.out.println("一个\\t制表符长度是8");
System.out.println("12345678def");
System.out.println("换行符 \\n");
System.out.println("abc\ndef");
System.out.println("单引号 \\'");
System.out.println("abc\'def");
System.out.println("双引号 \\\"");
System.out.println("abc\"def");
System.out.println("反斜杠本身 \\");
System.out.println("abc\\def");
}
}
创建字符串
package character;
public class TestString {
public static void main(String[] args) {
String garen ="盖伦"; //字面值,虚拟机碰到字面值就会创建一个字符串对象
String teemo = new String("提莫"); //创建了两个字符串对象
char[] cs = new char[]{'崔','斯','特'};
String hero = new String(cs);// 通过字符数组创建一个字符串对象
String hero3 = garen + teemo;// 通过+加号进行字符串拼接
}
}
immutable 是指不可改变的
比如创建了一个字符串对象
String garen =“盖伦”;
不可改变的具体含义是指:
不能增加长度
不能减少长度
不能插入字符
不能删除字符
不能修改字符
一旦创建好这个字符串,里面的内容 永远 不能改变
String 的表现就像是一个常量
创建一个长度是5的随机字符串,随机字符有可能是数字,大写字母或者小写字母
package character;
public class TestString {
public static void main(String[] args) {
//方法1
char cs[] = new char[5];
short start = '0';
short end = 'z'+1;
for (int i = 0; i < cs.length; i++) {
while (true) {
char c = (char) ((Math.random() * (end - start)) + start);
if (Character.isLetter(c) || Character.isDigit(c)) {
cs[i] = c;
break;
}
}
}
String result = new String(cs);
System.out.println(result);
//方法2
String pool = "";
//这里的short是用ASCII来计算的i返回的是ASCII值,然后将i用char转换就可得到祖父型的i了
for (short i = '0'; i <= '9'; i++) {
pool+=(char)i;
}
for (short i = 'a'; i <= 'z'; i++) {
pool+=(char)i;
}
for (short i = 'A'; i <= 'Z'; i++) {
pool+=(char)i;
}
char cs2[] = new char[5];
for (int i = 0; i < cs2.length; i++) {
int index = (int) (Math.random()*pool.length());
cs2[i] = pool.charAt( index );
}
String result2 = new String(cs2);
System.out.println(result2);
}
}
字符串数组的比较
package character;
import java.util.Arrays;
public class TestString {
public static void main(String[] args) {
String[] ss = new String[8];
for (int i = 0; i < ss.length; i++) {
String randomString = randomString(5);
ss[i] = randomString;
}
System.out.println("未排序前的字符串数组:");
System.out.println(Arrays.toString(ss));
for (int j = 0; j < ss.length; j++) {
for (int i = 0; i < ss.length - j - 1; i++) {
char firstChar1 = ss[i].charAt(0);
char firstChar2 = ss[i + 1].charAt(0);
firstChar1 = Character.toLowerCase(firstChar1);
firstChar2 = Character.toLowerCase(firstChar2);
if (firstChar1 > firstChar2) {
String temp = ss[i];
ss[i] = ss[i + 1];
ss[i + 1] = temp;
}
}
}
System.out.println("排序后的字符串数组:");
System.out.println(Arrays.toString(ss));
}
private static String randomString(int length) {
String pool = "";
for (short i = '0'; i <= '9'; i++) {
pool += (char) i;
}
for (short i = 'a'; i <= 'z'; i++) {
pool += (char) i;
}
for (short i = 'A'; i <= 'Z'; i++) {
pool += (char) i;
}
char cs[] = new char[length];
for (int i = 0; i < cs.length; i++) {
int index = (int) (Math.random() * pool.length());
cs[i] = pool.charAt(index);
}
String result = new String(cs);
return result;
}
}
我认为上边的那个冒泡比较还是有一点欠缺,因为假如首字母相同的话,对于字符串来说,还是要比较接下来的字符,所以可以用compareTo函数来比较两个字符串,
是否是同一个对象
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str2 = new String(str1);
//==用于判断是否是同一个字符串对象
System.out.println( str1 == str2);
}
}
返回值为false
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str3 = "the light";
System.out.println( str1 == str3);
}
}
返回值为true
使用equals进行字符串内容的比较,必须大小写一致
equalsIgnoreCase,忽略大小写判断内容是否一致
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str2 = new String(str1);
String str3 = str1.toUpperCase();
//==用于判断是否是同一个字符串对象
System.out.println( str1 == str2);
System.out.println(str1.equals(str2));//完全一样返回true
System.out.println(str1.equals(str3));//大小写不一样,返回false
System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写的比较,返回true
}
}
是否以子字符串开始或者结束
startsWith //以…开始
endsWith //以…结束
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String start = "the";
String end = "Ight";
System.out.println(str1.startsWith(start));//以...开始
System.out.println(str1.endsWith(end));//以...结束
}
}
返回值为 true
false
append追加
delete 删除
insert 插入
reverse 反转
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "let there ";
StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象
sb.append("be light"); //在最后追加
System.out.println(sb);
sb.delete(4, 10);//删除4-10之间的字符
System.out.println(sb);
sb.insert(4, "there ");//在4这个位置插入 there
System.out.println(sb);
sb.reverse(); //反转
System.out.println(sb);
}
}
为什么StringBuffer可以变长?
和String内部是一个字符数组一样,StringBuffer也维护了一个字符数组。 但是,这个字符数组,留有冗余长度
比如说new StringBuffer(“the”),其内部的字符数组的长度,是19,而不是3,这样调用插入和追加,在现成的数组的基础上就可以完成了。
如果追加的长度超过了19,就会分配一个新的数组,长度比原来多一些,把原来的数据复制到新的数组中,看上去 数组长度就变长了 参考MyStringBuffer
length: “the”的长度 3
capacity: 分配的总空间 19
注: 19这个数量,不同的JDK数量是不一样的
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the";
StringBuffer sb = new StringBuffer(str1);
System.out.println(sb.length()); //内容长度
System.out.println(sb.capacity());//总空间
}
}