简体   繁体   English

如何删除从Excel导入的字符串中的空格

[英]How to remove whitespace in String imported from Excel

I need to remove all white character from a string and I am not able to do so. 我需要从字符串中删除所有白色字符,但无法删除。 Anyone has an idea on how to do it? 有人对如何做有想法吗?

Here is my string retrieved from an excel file via jxl API : 这是我通过jxl API从excel文件中检索的字符串:

"Destination à gauche"

And here are its bytes : 这是它的字节数:

6810111511610511097116105111110-96-32321039711799104101

There is the code I use to remove whitespaces : 我有用于删除空格的代码:

public static void checkEntetes(Workbook book) {
        String sheetName = "mysheet";
        System.out.print(sheetName + " : ");
        for(int i = 0; i < getColumnMax(book.getSheet(sheetName)); i++) {
            String elementTrouve = book.getSheet(sheetName).getCell(i, 0).getContents();
            String fileEntete = new String(elementTrouve.getBytes()).replaceAll("\\s+","");
            System.out.println("\t" + elementTrouve + ", " + bytesArrayToString(elementTrouve.getBytes()));
            System.out.println("\t" + fileEntete + ", " + bytesArrayToString(fileEntete.getBytes()));
        }
        System.out.println();
}

And this outputs : 这输出:

"Destination à gauche", 6810111511610511097116105111110-96-32321039711799104101
"Destination àgauche", 6810111511610511097116105111110-96-321039711799104101

I even tried to make it myself and it still leaves a space before the 'à' char. 我什至尝试自己制作,但在'à'char之前仍留有空格。

public static String removeWhiteChars(String s) {
    String retour = "";
    for(int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if(c != (char) ' ') {
            retour += c;
        }
    }
    return retour;
}

regular expressions to the rescue: 正则表达式:

str = str.replaceAll("\\s+", "")

will remove any sequence of whitespace characters. 将删除任何空白字符序列。 for example: 例如:

String input = "Destination à gauche";
String output = input.replaceAll("\\s+","");
System.out.println("output is \""+output+"\"");

outputs Destinationàgauche 输出Destinationàgauche

if youre starting point is indeed the raw bytes ( byte[] ) you will first need to make them into a String: 如果您的起点确实是原始字节( byte[] ),则首先需要将它们设置为String:

byte[] inputData = //get from somewhere
String stringBefore = new String(inputData, Charset.forName("UTF-8")); //you need to know the encoding
String withoutSpaces = stringBefore.replaceAll("\\s+","");
byte[] outputData = withoutSpaces.getBytes(Charset.forName("UTF-8"));

If you would like to use a formula, the TRIM function will do exactly what you're looking for: 如果您想使用公式,则TRIM函数将完全满足您的要求:

+----+------------+---------------------+
|    |     A      |           B         |
+----+------------+---------------------+
| 1  | =TRIM(B1)  |  value to trim here |
+----+------------+---------------------+

So to do the whole column. 因此要做整列。 1) Insert a column 2) Insert TRIM function pointed at cell you are trying to correct. 1)插入一列2)插入指向要纠正的单元格的TRIM函数。 3) Copy formula down the page 4) Copy inserted column 5) Paste as "Values" 3)在页面下复制公式4)复制插入的列5)粘贴为“值”

Reference: Question number 9578397 on stackoverflow.com 参考:在stackoverflow.com上的问题编号95778397

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM