-
Java 出现unreachable statement异常
一个unreachable statement就是编译器决定永远不会执行它。 下面的几种情况会出现 unreachable statement: (1)在reutrn语句后写语句。 (2)在throw语句后写语句。 (3)break、continue语句之后定义语句。
-
如何将整数 int 转换成字串 String ?
1、String s = String.valueOf(i); 2、String s = Integer.toString(i); 3、String s = "" + i; 注: Double, Float, Long 转成字串的方法大同小异.
-
如何将字串 String 转换成整数 int?
int i = Integer.parseInt([String]);
-
Java去掉字符串前面的0
String str = "000000001234034120"; String newStr = str.replaceAll("^(0+)", "");//使用正则表达式 System.out.println(newStr);
public static void main(String[] args) { String str = "00001102";// 测试用字符串 int len = str.length();// 取得字符串的长度 int index = 0;// 预定义第一个非零字符串的位置 char strs[] = str.toCharArray();// 将字符串转化成字符数组 for (int i = 0; i < len; i ++) { if ('0' != strs[i]) { index = i;// 找到非零字符串并跳出 break; } } String strLast = str.substring(index, len);// 截取字符串 System.out.println(strLast);// 得到结果 strLast }
- Java字符串大小比较——compareTo()方法
语法:str1.compareTo(str2);
其返回的是一个int类型值。若Str1等于参数字符串Str2字符串,则返回0;若该Str1按字典顺序小于参数字符串Str2,则返回值小于0;若Str1按字典顺序大于参数字符串Str2,则返回值大于0。Java中的compareTo()方法,返回参与比较的前后两个字符串的ASCII码的差值。
*****************************************2020/10/16************************************