字符串操作和Math类

字符串操作:

  字符串时对象,对它的所有操作都是通过 “ . ” 这个运算符来进行的

  字符串 . 操作 

  它表示对 . 左边的这个字符串做右边的那个操作

  这里的字符串有是变量也可以是常量、

public static void main(String[] args) {
    String a = "112";
    String b = "221";
    System.out.println(a.compareTo(b));
}

 

 

 说明b比a要大,如果是1的话就是说明a要比b大,如果是0的话那么这两个数就是相等的

String a = "112";
for (int i = 0; i <a.length(); i++) {
    System.out.println(a.charAt(i));
}

 

得到子串

s.substring(1)
  得到从1号位置到末尾的全部内容
s.substring(1,3)
  得到从1号位置到3号位置之前的内容

String a = "1121112";
System.out.print(a.substring(2));
System.out.println();
System.out.print(a.substring(2,5));

 

  从右边开始找

String a = "135421596";
System.out.print(a.indexOf('2'));

结果是四就是这个2在上面的位置在第从0开始到第4个

 

 

在swirch-case中使用字符串

   switch ( s ) i
     case "this":...break;
     case "that:...break;
  }

 

Math

给了我们一些很方便的数学操作

复制代码
  //求绝对值
   System.out.println(Math.abs(-12));
  //四舍五入 System.
out.println(Math.round(10.645));
  //0到100的随机数   System.
out.println(Math.random()*100);
  //2的3.2次方 System.
out.println(Math.pow(2,3.2));

这是结果

12
11
3.1269725048264085
9.18958683997628

复制代码

 

posted @   漁夫  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示