java实现去除字符串中的空格(附完整源码)
时间: 2024-02-18 20:05:59 浏览: 32
Java 中可以使用 String 类的 replaceAll() 方法来去除字符串中的空格。下面是示例代码:
```java
public class RemoveSpacesExample {
public static void main(String[] args) {
String strWithSpaces = " hello world ";
String strWithoutSpaces = strWithSpaces.replaceAll("\\s", "");
System.out.println("Before: " + strWithSpaces);
System.out.println("After: " + strWithoutSpaces);
}
}
```
在这段代码中,我们首先定义了一个包含空格的字符串 strWithSpaces,然后使用 replaceAll() 方法将其中的空格替换为空字符串。这里需要注意的是,replaceAll() 方法的第一个参数是一个正则表达式,这里我们使用了 "\\s" 表示匹配任何空白字符,包括空格、制表符和换行符。
输出结果如下:
```
Before: hello world
After: helloworld
```
这样就去除了字符串中的空格。
相关问题
java实现去掉字符串中指定的字符(附完整源码)
以下是Java实现去掉字符串中指定的字符的完整源码:
```java
public class RemoveChar {
public static void main(String[] args) {
String input = "Hello, World!";
char charToRemove = 'o';
String output = removeChar(input, charToRemove);
System.out.println(output);
}
public static String removeChar(String input, char charToRemove) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != charToRemove) {
output.append(c);
}
}
return output.toString();
}
}
```
在这个程序中,我们首先定义一个字符串input和一个要去除的字符charToRemove。然后,我们调用removeChar方法去掉字符串中指定的字符,并将结果输出。
在removeChar方法中,我们使用StringBuilder类创建一个空的字符串output,然后遍历输入字符串input中的每个字符。如果当前字符不是要去除的字符charToRemove,我们将它添加到output字符串中。最后,我们返回output字符串。
需要注意的是,如果要去除的字符可能在字符串中出现多次,可以使用replace方法对输入字符串进行替换。
java实现字符串反转(附完整源码)
下面是Java实现字符串反转的完整源码:
```java
public class StringReverse {
public static void main(String[] args) {
String str = "Hello World";
String reversed = reverseString(str);
System.out.println("Original String: " + str);
System.out.println("Reversed String: " + reversed);
}
public static String reverseString(String str) {
if (str == null || str.length() == 0) {
return str;
}
char[] chars = str.toCharArray();
int i = 0;
int j = chars.length - 1;
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
return new String(chars);
}
}
```
在该代码中,我们定义了一个名为 `StringReverse` 的类,包含了一个 `main` 方法和一个 `reverseString` 方法。
`main` 方法中定义了一个字符串 `str`,并调用 `reverseString` 方法对其进行反转。最后,我们打印出原始字符串和反转后的字符串。
`reverseString` 方法首先检查字符串是否为空或长度为零,如果是,则返回原始字符串。然后,我们将字符串转换为字符数组,并定义两个指针 `i` 和 `j`,分别指向数组的开头和结尾。在循环中,我们将 `i` 指向的字符与 `j` 指向的字符进行交换,然后 `i` 指针向右移动,`j` 指针向左移动,直到 `i` 大于等于 `j` 为止。最后,我们将字符数组转换回字符串并返回它。
以上就是Java实现字符串反转的完整源码,希望对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)