[Solved] Solution to the problem of using trim() to remove spaces when importing excel in Java

1. Problem description

During the excel import operation, when reading the string in the cell, after obtaining the string in the cell, I used the trim() method of string to remove the leading and trailing spaces, and found that it could not be removed no matter what.

2. Cause analysis

After searching for a while, I found that it was due to the input method's full-width and half-width spaces. The trim() method can only remove spaces with an ASCII value of 32 in the string, that is, half-width spaces, but cannot remove full-width spaces.
In other words, trim or replace can only remove spaces with an ASCII value of 32, while spaces with an ASCII value of 160 or full-width spaces cannot be processed by replace or trim: some are called Chinese spaces;
half-width spaces: some are called is an English space.
For example:
after turning on full-width, the content of the input method is as follows: abcd 
Half-width input is as follows: abcd, the input method is half-width by default.
How to switch to full half-width: shift+space

Insert image description here

3. Solution

Option 1: Use regular expressions

The ASCII encoding is 160 spaces, and the Unicode equivalent is \u00A0. Use replace+UNICODE encoding \u00A0 to replace, full-width spaces (\u3000), use the replace method + full-width space characters to replace, ordinary spaces, use the replace method + \s to replace, regular Expression.replaceAll("([ ]|\s|\u00A0)+","")

as follows:


public class Demo1 {
    
    
  public static void main(String[] args) {
    
    
 		String s2 = "全角空格测试 !!";
        System.out.println(s1.replaceAll("([ ]|\\s|\\u00A0)+",""));
        // 全角空格测试!!
  }
}

Option 2: Use String.strip()

My problem here is caused by full-width spaces, so you can use String.strip() to remove the "full-width and half-width" whitespace characters before and after the string.

public class Demo1 {
    
    
  public static void main(String[] args) {
    
    
    String s3 = "吃了没\u3000";
    System.out.println(s3.trim().length());
    System.out.println(s3.strip().length());
    // 4 3
    System.out.println(s3.strip());
    // "吃了没"
  }


Note: String.strip() was introduced in JDK11. If your project uses an environment below JDK11, this method cannot be used.

Option 3: Use hutool’s StrUtil.trim() method

You can use the tool class StringUtils.trimWhitespace() provided by spring to remove full-width spaces in the string.
The code is as follows:

/**@Description: 全角 空格去除测试
 */
@Test
public void test2(){
    
    
    String sLeft = " 左全角空格";
    String sMid = " 两边 全角空格  ";
    String sRight = "右边全角空格  ";
 
    System.out.println("String工具类 left:"+ org.springframework.util.StringUtils.trimTrailingWhitespace(sLeft));
    System.out.println("String工具类 mid:"+ org.springframework.util.StringUtils.trimTrailingWhitespace(sMid));
    System.out.println("String工具类 right:"+ org.springframework.util.StringUtils.trimTrailingWhitespace(sRight));
 
    // 使用 hutool的 StrUtil
 
    System.out.println("Hutool StrUtil 工具类 left:"+StrUtil.trim(sLeft));
    System.out.println("Hutool StrUtil 工具类 mid:"+StrUtil.trim(sMid));
    System.out.println("Hutool StrUtil 工具类 right:"+StrUtil.trim(sRight));
 
}
/**@Description:Hutool StrUtil 去除普通半角空格
 * <br> 半角空格=英文空格; 全角空格=中文空格
 */
@Test
public void  test3(){
    
    
    String sLeft = "  左半角  右全角  ";
    System.out.println("原始字符串:"+sLeft);
 
    System.out.println("hutool :"+StrUtil.trim(sLeft));
    System.out.println("StringUtil :"+StringUtils.trim(sLeft));
    System.out.println("Spring StringUtil :"+ org.springframework.util.StringUtils.trimWhitespace(sLeft));
 
    // 去掉 字符串中全部空格
    System.out.println(org.springframework.util.StringUtils.trimAllWhitespace(sLeft));
 
    sLeft = StrUtil.trim(sLeft);
    System.out.println("最终去除效果:"+sLeft);
}

4. Summary

1. The trim method in Java's String class can only remove half-width spaces. If the spaces cannot be removed, you can consider using hutool or the tool class provided by spring to try out possible full-width spaces.

2. The pom dependencies of hutool are:

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.7.18</version>
</dependency>

Note: The StringUtils tool class in apache-commons-lang3 cannot remove full-width spaces because the trim() method of the String class is actually called.

Guess you like

Origin blog.csdn.net/m0_37899908/article/details/131385930