Java中使用频率较高的函数

1.常用数学函数

            Math.abs(±7.5);            //返回绝对值  结果:7.5
        
        Math.copySign(7, -5);      //返回第一个参数的绝对值,第二个参数的符号  结果:-7
        
        Math.signum(-7.5);         //正数返回1.0,负数返回-1.0,0返回0  结果:-1.0
        
        Math.exp(0);               //e的0次幂  结果:1
        Math.expm1(1);             //e的-1次幂  结果:-e        

        Math.log(e);               //以e为底的对数  结果:1

        Math.ceil(-7.5);           //返回最近的且大于这个数的整数  结果:-7
        Math.floor(-7.5);          //返回最近的且小于这个数的整数  结果:-8        

        Math.rint(7.4);            //返回最近的整数  结果:7
        Math.rint(7.5);            //若是中间数则取偶数  结果:8
 
Math.hypot(3, 4); //返回两数和的平方根结果:5

Math.sqrt(9); //返回该数的平方根 结果:3
Math.cbrt(27.0); //返回该数的立方根结果:3

Math.max(7, 5); //返回7、5中较大的那个数 结果:7
Math.min(7, 5); //返回7、5中较小的那个数结果:5

Math.pow(2, 3); //返回2的3次幂 结果:8

Math.random(); //产生随机数


2.String相关函数

charAt():

        char ch;
     ch=”abc”.charAt(1);              //返回第1个字符  结果:a
substring():

        String s1 ="1234567890abcdefgh";
        s1 = s1.substring(10);            //返回从第10个数到末尾的子串  结果:abcdefgh   

        String s1 ="1234567890abcdefgh";
        s1 = s1.substring(10,11);         //返回从第10个子串  结果:a
concat():

           String s="Welcome to ";
      String t=s.concat("NanJing");       //连接两个字符串  结果:Welcome to NanJing

replace():

           String m="Hello";
        String n=m.replace('l', 'h');
        System.out.println(n);              //将字符串中所有字符'l'替换成'h'  结果:hehho 

toLowerCase()/toUpperCase():

            String m="HEllo";
        String n=m.toLowerCase();
        System.out.println(n);              //将字符串中所有字符变成小写  结果:hello
        String i=m.toUpperCase();
        System.out.println(i);              //将字符串中所有字符变成大写  结果:HELLO

length():

            char chars[]={'a','b','c'};
	String s=new String(chars);
	int len=s.length();
        System.out.println(len);             //获取字符串的长度  结果:3
valueOf():

           char chars[]={'a','b','c'};
	String value=String.valueOf(chars);
        System.out.println(value);           //转化为字符串  结果:abc
trim():

            String chars=" abcdefg ";
        int len1=chars.length();
        System.out.println(chars+"   长度为:"+len1);    
        //返回原字符串和其长度   结果: abcdefg    长度为:9
      
        String value=chars.trim();
        int len2=value.length();
        System.out.println(value+"   长度为:"+len2);       
        //返回去掉空格后的字符串和其长度   结果:abcdefg   长度为:7

getChars():

        String s="this is a demo of the getChars method";
        char buf[]=new char[4];
        s.getChars(10,14,buf,0);           
        //返回原字符串从第10位到第14位的子串,存至数组buf,字符串前偏移量为0
        System.out.println(buf);    //结果:demo

equals():

            String str1 = new String("hello");
        String str2 = new String("hello");
        System.out.println("str1和str2是否相等?"+str1.equals(str2));
        //判断两个字符串是否相等  结果:str1和str2是否相等?true



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值