java常用的几种函数
math类常用的几个函数
Math.E
System.out.println(Math.E);//一般自然常量
Math.PI
System.out.println(Math.PI);//圆周率
math.abs()
System.out.println(Math.abs(-5));//求绝对值
Math.ceil(double a);
System.out.println(Math.ceil(3.4));//大于该数字的最小整数
Math.floor(double a);
System.out.println(Math.floor(3.4));//小于该数字的最大整数
Math.hypot(double x,double y);
double x1=5.7;
double x2=5.9;
double y2=5.4;
double y1=4.8;
double x= x2 - x1;
double y=y2-y1;
System.out.println(Math.hypot(x, y));//求两点之间的距离
Math.max(a,b);
System.out.println(Math.max(1, 9));//返回两者中最大数
Math.min(a,b);
System.out.println(Math.min(1, 9));//返回两者中最小数
Math.sqrt(a)
System.out.println(Math.sqrt(4));//求开方
Math.rint(a);
System.out.println(Math.rint(3.4));//四舍五入
Math.round();
System.out.println(Math.round(3.8));//四舍五入
String不是基本数据类型而是一个类,一个对象,那么就会有自己的方法
String s;
s.length();//打印字符串的长度
String s="123456";
System.out.println(s.length());//打印字符串的长度
查询类的方法
s.charAt(i);//返回char
String s="123456";//角标是从0开始
System.out.println(s.charAt(0));//返回角标是0的那个字符(char)
s.indexOf(s1)//返回的是角标
System.out.println("abc".indexOf("a"));//查询字符a在字符串abc中的角标
s.lastIndexOf();
System.out.println("abcd".lastIndexOf("a"));
s.subString(beginindex,endindex);//endindex是取不到的
System.out.println("abcd".substring(0,3));//从角标0开始截取字符串到3,[0,3)打印的就是0-2的字符串
判断类的方法
s.contains(s1);//判断子串是否包含在s中
String s="123456";//角标是从0开始
System.out.println(s.contains("123"));//返回boolean
s.endWith(s1);//判断子串是否在s的结尾
String s="123456";//角标是从0开始
System.out.println(s.endsWith("456"));//返回boolean
s.startWith(s1);
System.out.println("abcd".startsWith("ab"));//查询字符串abc是否以ab开始,结果返回true或者false
s.compareTo(s1)//按照字典顺序比较两个字符串,返回值是整数,返回是0,表示相等,返回其他值说明不相等
System.out.println("456".compareTo("456"));
s.equals();//只表示两个字符串的内容 相等true 不等为false
System.out.println("456".equals("456"));
s.equalsIgnoreCase(s1);//忽视大小写进行比较
System.out.println("abc".equalsIgnoreCase("Abc"));
boolean isEmpty();
System.out.println("".isEmpty());//查询字符串是否为空
字符串的修改
注意啦!对字符串的修改都不是对字符串本身的修改,因为字符串本身是不可变的,往往是新建一个字符串,修改内容或者赋值后返回新的字符串。
s.replace(oldchar,newchar);
System.out.println("abcd".replace('a','m'));
s.toUpperCase();//将字符串中所有的字母转大写
System.out.println("abcd".toUpperCase());//将字符串中所有的字母转大写
s.toLowerCase();//将字符串中所有字母转小写
System.out.println("abcd".toLowerCase());//将字符串中所有的字母转小写
s.trim();//去掉字符串中前后的空格
System.out.println(" ab cd ".trim());//去掉字符串中前后的空格
下面来看下利用字符串函数写的编程题
subString();
/*
求s2在s1中出现的次数
*/
class zifuchuan11{
public static void main(String[] args){
String s1="acbcsbddabchhjavb";
String s2="abc";
int count=0;
for(int i=0;i<s1.length()-s2.length()+1;i++){
String sub=s1.substring(i,i+s2.length());
if(sub.equals(s2)){
count++;
}
}
System.out.print("s2在s1中出现了 "+count);
}
}
判断字符串是否是回文
class zifuchuan11{
public static void main(String[] args){
String s1="abcdcba";
int left=0;
int right=s1.length()-1;
boolean flag=true;
while(true){
if(s1.charAt(left)==s1.charAt(right)){
left++;
right--;
if(left>=right){
break;
}
}
else {
flag=false;
break;
}
}
System.out.print("s1是回文吗 "+flag);
}
}
模拟trim功能,自定义实现
/*
模拟trim()的功能,自定义实现
*/
class zifuchuan11{
public static void main(String[] args){
String s1=" abcdcba ";
int left=0;
int right=s1.length()-1;
while(s1.charAt(left)==' '){
left++;
}
while(s1.charAt(right)==' '){
right--;
}
String substr=s1.substring(left,right+1);
System.out.print(substr);
}
}
这个有点难啊
/*
找出s1="Python is a program language but is slow"和
s2="java is a program language but is fast"共有最长字符串
*/
class zifuchuan11{
public static void main(String[] args){
String s1="Python is a program language but is slow";
String s2="java is a program language but is fast";
boolean flag=true;
for(int len=s2.length();len>=1;len--){
for(int i=0, j=len-1;j<s2.length();i++,j++){
String sub=s2.substring(i,j+1);
if(s1.contains(sub)){
System.out.print(sub);
flag=false;
break;
}
}
if(!flag){
break;
}
}
}
}