-
验证ean-13条码的验证码是否正确 String code = “6921168509256”; 取前12位的奇数位的和 取前12位的偶数位的和 将"奇数位和"与"偶数位和的三倍"相加求和 取和的个位数,然后用10减去这个个位数,相减的结果就是条码的验证码 如果条码的第13位数字和这个验证码相同就说明验证码正确
public void test1(){
String code = "6921168509256";
String[] split = code.substring(0, 12).split("");
// 取前12位的奇数位的和
// 取前12位的偶数位的和
int sumJ = 0;
int sumO = 0;
for (int i = 0; i < split.length; i++) {
if(i%2==0)
sumJ+=Integer.parseInt(split[i]);
else sumO += Integer.parseInt(split[i]);
}
System.out.println("奇数位的和 偶数位的和分别为"+sumJ+"--"+sumO);
// 将"奇数位和"与"偶数位和的三倍"相加求和
// 取和的个位数,然后用10减去这个个位数,相减的结果就是条码的验证码
int temp = 10-(sumJ+sumO*3)%10;
System.out.println(temp);
// 如果条码的第13位数字和这个验证码相同就说明验证码正确
if(temp == (code.charAt(code.length()-1)-'0'))
System.out.println("验证码正确");
else System.out.println("验证码错误");
}
public String randomString(int length){
if(length<=0)
return "";
char[] chars = new char[length];
char temp;
int t;
Random random = new Random();
for (int i = 0; i < chars.length; i++) {
t = random.nextInt(63)+1;
if(t<=26){
temp = (char)((int)'a'+t-1);
}else if(t<=52)
temp = (char)((int)'A'+t-27);
else if(t<=62)
temp = (char)((int)'0'+t-1);
else temp='_';
chars[i] = temp;
}
return String.copyValueOf(chars);
}
@Test
public void test2(){
System.out.println(randomString(100));
}
public String replace(String str,char ch){
StringBuilder stringBuilder = new StringBuilder(str);
int i = str.lastIndexOf(ch);
if(str.charAt(i)<='Z'){
stringBuilder.setCharAt(i,(char)(str.charAt(i)+36));
}else stringBuilder.setCharAt(i,(char)(str.charAt(i)-36));
return stringBuilder.toString();
}
@Test
public void test3(){
String str = "hello";
String replace = replace(str, 'l');
System.out.println(replace);
System.out.println(str);
}
-
已知字符串:“this is a test of java”. 按要求执行以下操作:
(1) 【简】统计该字符串中字母s出现的次数
public void test4(){ String code = "this is a test of java"; char[] chars = code.toCharArray(); int count = 0; for (char aChar : chars) { if(aChar=='s') count++; } System.out.println(count); }
(2) 【简】取出子字符串"test"
public void test5(){ String code = "this is a test of java"; System.out.println(code.substring(10,14)); }
(3) 【简】将本字符串复制到一个字符数组Char[] str中.
public void test6(){ String code = "this is a test of java"; char[] chars = code.toCharArray(); System.out.println(Arrays.toString(chars)); }
(4) 【中】将字符串中每个单词的第一个字母变成大写, 输出到控制台。
public void test7(){ String code = "this is a test of java"; String[] split = code.split(" "); String newcode = ""; System.out.println(Arrays.toString(split)); for (String s : split) { newcode = newcode + s.substring(0, 1).toUpperCase()+s.substring(1, s.length())+" "; } newcode = newcode.substring(0,newcode.length()-1); System.out.println(newcode); }
-
设计一个方法,提取一个身份证号码中的生日部分。身份证号的验证规则,只需 要验证前17位是数字,最后一位可以数字或者X即可。
public static void main(String[] args) {
String id = "3624222000032122";
if(isId(id)){
System.out.println("生日部分为"+id.substring(10,14));
}else {
System.out.println("身份证格式有误");
}
}
public static boolean isId(String id){
String checking = "\\d{17}[\\d|x|X]";
System.out.println(id.matches(checking));
return id.matches(checking);
}
-
【中】设计⼀个⽅法,将如下字符串中的所有⼿机号码和固话号码提取出来,存⼊⼀个数组返回。
7.1⼿机号码的校验: ⾸位是1,第⼆位是 3579,后⾯都是数字
public static void main(String[] args) { String checking = "1[3579]\\d{9}"; String phoneNum = "13907371231"; System.out.println(phoneNum.matches(checking)); }
7.2 固化校验: 区号-号码,其中区号3或4位,号码7位,且⾸位不能是0
public static boolean checkingPhone(String num){ String checking = "\\d{3,4}-[^0]\\d{6}"; return num.matches(checking); }
7.3 待提取数据的字符串: 17788889999-010-9988776-19822883322-0398-8976564-16722882288- 0922-78906543-15490876845-010-99821123
public static String getPhoneNum(String str){ String checking = "1[3579]\\d{9}|\\d{3,4}-[^0]\\d{6}"; Pattern compile = Pattern.compile(checking); Matcher matcher = compile.matcher(str); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()){ stringBuffer.append(matcher.group()+", "); } return stringBuffer.toString(); }
-
⽐较String和StringBuffer在⼤字符串操作⽅⾯的性能差异, ⽐如增加1万个单 词,⽐较各⾃所⽤的时间
public static void main(String[] args) { String word = ""; String t = "apple"; StringBuilder stringBuilder = new StringBuilder(); StringBuffer stringBuffer = new StringBuffer(); int i = 100000; long start = System.currentTimeMillis(); while (i!=0){ word+=t; i--; } long end = System.currentTimeMillis(); Long stringT = end-start; i=100000; start = System.currentTimeMillis(); while (i!=0){ stringBuffer.append(t); i--; } end = System.currentTimeMillis(); Long stringBufferT = end-start; i=100000; start = System.currentTimeMillis(); while (i!=0){ stringBuilder.append(t); i--; } end = System.currentTimeMillis(); Long stringBuildT = end-start; System.out.println(stringT+"---"+stringBufferT+"----"+stringBuildT); }