Concat all strings without digits
# Topic
StringBuilder
# Problem
# Concat all strings without digits
Implement a method to concatenate all strings from the given array to a single long string. You must skip all digits inside the input strings.
Use StringBuilder
to solve the problem, because the input array can contain a huge number of strings.
Sample Input 1:
T7est i1nput
1
Sample Output 1:
Testinput
1
# Hint & Explain
Use ".replaceAll("\d","");" method!
# Solution
# Other solution 1
import java.util.Scanner;
class ConcatenateStringsProblem {
public static String concatenateStringsWithoutDigits(String[] strings) {
// write your code with StringBuilder here
StringBuilder join = new StringBuilder();
for (String string : strings) {
join.append(string.replaceAll("\\d", ""));
}
return join.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] strings = scanner.nextLine().split("\\s+");
String result = concatenateStringsWithoutDigits(strings);
System.out.println(result);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Other solution 2
import java.util.Scanner;
class ConcatenateStringsProblem {
public static String concatenateStringsWithoutDigits(String[] strings) {
StringBuilder word = new StringBuilder();
for (String str : strings) {
word.append(str);
}
return String.valueOf(word).replaceAll("\\d", "");
// write your code with StringBuilder here
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] strings = scanner.nextLine().split("\\s+");
String result = concatenateStringsWithoutDigits(strings);
System.out.println(result);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编辑 (opens new window)
上次更新: 2022/09/25, 10:41:23