今日学习:数学函数,子串的提取、连接。
1.数学函数sqrt,pow
public class DoubleYs {
public static void main(String []args) {
double x = 4;
double y = Math.sqrt(x);
double z = Math.pow(x,2);
System.out.println(y);
System.out.println(z);
}
}
2.子串提取
String类的substring方法可以从长的子串中提取比较短的子串
public class Zichuan {
public static void main(String []args) {
String greeting = “hello”;
String s = greeting.substring(0,3); //提取短的子串,提取0,1,2三个位置的值
System.out.println(s);
// System.out.println(z);
}
}
结果:hel
3.两个字符串连接 :用“+”即可实现
public class Zichuan_lj {
public static void main(String []args) {
String str1 = “hello”;
String str2 = " word!";
String s = str1 + str2 ; //连接两个子串
System.out.println(s);
// System.out.println(z);
}
}
4.检测两个字符串是否相等
用equals方法来检测
s.equals(t)来比较子串s和t
5.子串的长度
public class Length_zf {
public static void main(String []args) {
String str1 = “hello”;
// String str2 = " hello";
int n = str1.length(); //结果为5
int cpCount = str1.codePointCount(0,str1.length()); //实际长度,也是代码点数量
System.out.println(n);
System.out.println(cpCount);
}
}
其它知识点,先记录下来: