0%

模拟-字符串操作

题目一 字符串中大小写转换

toUpperCase方法

1
2
String str = scan.nextLine();
str = str.toUpperCase();

题目二:字符串转字符数组

toCharArray

1
2
3
4
5
6
String str = scan.nextLine();
for(char c : str.toCharArray()) { // 修改这里
if(c != ' ') { // 修改这里
count++;
}
}

题目三:字符串和数字之前的转换

字符串转数字:数字的类类型.parse数字的类类型(字符串)

1
int i = Integer.parseInt(str);

数字转字符串:String.valueOf(数字)

1
String str = String.valueOf(i);

判断一个字符在不在字符串中:字符串.indexOf(字符),如果不在返回-1

1
str.indexOf('9') 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
//在此输入您的代码...
int sum = 0;
for(int i = 1; i <= 2019; i++) {
String str = String.valueOf(i);
if(str.indexOf('2') != -1||
str.indexOf('0') != -1||
str.indexOf('1') != -1||
str.indexOf('9') != -1) {
int a = Integer.parseInt(str);
sum += a;
}
}
System.out.print(sum);
}
}

题目四 串的处理

输入

1
you and me what cpp2005program

输出

1
You And Me What Cpp_2005_program

字符串

将一个字符串分割成多个子字符串构成字符串数组

1
String[] strLiStrings = str.split("\\s+")

不同单词,其中不同单词之间的空格不止一个

访问字符串指定位置字符

1
subString.charAt(i)

可变字符串

声明

1
StringBuffer stringBuffer = new StringBuffer();

在后面加入

1
stringBuffer.append()

在指定位置插入

1
stringBuffer.insert(i, '_')

删除指定位置字符串

1
stringBuffer.delete(0, 2);  #左闭右开

字符

大小写转换

1
Character.toUpperCase(subString.charAt(0)

判断字符是否是数字

1
Character.isDigit(subString.charAt(i))

判断字符是否是字母

1
Character.isAlphabetic(subString.charAt(i))

代码一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
String str = scan.nextLine();
String[] strLiStrings = str.split("\\s+");
for(String subString : strLiStrings) {
//声明一个可变字符串
StringBuffer stringBuffer = new StringBuffer();
//首字母大写
stringBuffer.append(Character.toUpperCase(subString.charAt(0)));
//消除多余的空格
char pre = ' ';
char now = ' ';
for(int i = 0; i < subString.length(); i++) {
now = subString.charAt(i);
if(i != 0) {
if(pre == ' ' && now == ' ') {
continue;
} else {
stringBuffer.append(now);
}
} //if
pre = now;
}
//数字和字符之间加入_
pre = ' ';
now = ' ';
for(int i = 0; i < stringBuffer.length(); i++) {
now = stringBuffer.charAt(i);
if(i != 0) {
if(Character.isDigit(pre) && Character.isAlphabetic(now) || Character.isAlphabetic(pre) && Character.isDigit(now)) {
stringBuffer.insert(i, '_');
} //if
} //if
pre = now;
} //for
System.out.print(stringBuffer);
System.out.print(' ');
}
scan.close();
}
}

代码二:正则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String str[] = s.split("\\s+");
for (int i = 0; i < str.length; i++) {
str[i] = str[i].substring(0, 1).toUpperCase() + str[i].substring(1);
str[i] = str[i].replaceAll("(\\d)([a-zA-Z])", "$1_$2");
str[i] = str[i].replaceAll("([a-zA-Z])(\\d)", "$1_$2");
System.out.print(str[i] + " ");
}
}
}

题目五:进制转换

题目

将十进制数字转换成二进制字符串

1
String binaryString = Integer.toBinaryString(test)

将十进制数字转换成八进制字符串

1
String octalString = Integer.toOctalString(test)

将十进制数字转换成十六进制字符串

1
String hexString = Integer.toHexString(test)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Main {
public static void main(String[] args) {
int count = 10;
int test = 127;
int ans = 126;
while(count <= 2022) {
int sum = 0;
//先判断十进制
String demicalString = String.valueOf(test);
for(int i = 0; i < demicalString.length(); i++) {
sum += Character.getNumericValue(demicalString.charAt(i));
}
if(test % sum != 0) {
test++;
continue;
}
//判断二进制
sum = 0;
String binaryString = Integer.toBinaryString(test);
for(int i = 0; i < binaryString.length(); i++) {
sum += Character.getNumericValue(binaryString.charAt(i));
}
if(test % sum != 0) {
test++;
continue;
}
//判断八进制
sum = 0;
String octalString = Integer.toOctalString(test);
for(int i = 0; i < octalString.length(); i++) {
sum += Character.getNumericValue(octalString.charAt(i));
}
if(test % sum != 0) {
test++;
continue;
}
//判断十六进制
sum = 0;
String hexString = Integer.toHexString(test);
for(int i = 0; i < hexString.length(); i++) {
if(Character.isDigit(hexString.charAt(i))) {
sum += Character.getNumericValue(hexString.charAt(i));
} else {
sum += hexString.charAt(i) - 'a' + 10;
}
}
if(test % sum == 0) {
ans = test;
count++;
// System.out.format("%d %d\n", ans, count);
}
test++;
}
System.out.print(ans);
}
}

题目六 字符串的拼接

输入1

1
0-670-82162-4

输出1

1
Right

输入2

1
0-670-82162-0

输出2

1
0-670-82162-4 

判断两个字符串内容是否相等

1
str1.equals(str2)

将字符串数组用指定字符拼接成一个字符串

1
String str = String.join("-", strList);

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
String str = scan.nextLine();
String[] strList = str.split("-");
String yanzhen = "";
for(int i = 0; i < strList.length - 1; i++) {
yanzhen += strList[i];
}
int sum = 0;
//System.out.format("yanzhen = %s\n", yanzhen);
for(int i = 0; i < yanzhen.length(); i++) {
int nowNum = yanzhen.charAt(i) - '0';
//System.out.format("nowNum = %d, i + 1 = %d\n", nowNum, i + 1);
sum += nowNum * (i + 1);
//System.out.format("sum = %d\n", sum);
}
String last = "";
if(sum % 11 < 10) {
last = String.valueOf(sum % 11);
} else {
last = "X";
} //if
// System.out.format("last = %s\n", last);
// System.out.format("strList[strList.length - 1] = %s\n", strList[strList.length - 1]);
if(strList[strList.length - 1].equals(last)) {
System.out.print("Right");
} else {
strList[strList.length - 1] = last;
System.out.println(String.join("-", strList));
}
scan.close();
}
}