简体   繁体   English

在存储数据库之前,删除String数组的最后一个空字符

[英]remove a null character in last of String array before storing database

I am Storing Values from String Matrix (which is filled from a text file) to DataBase Table. 我正在将值从字符串矩阵(从文本文件填充)存储到数据库表中。 everything is working fine,but the values that are stored in Database have blank space after each word. 一切正常,但是存储在数据库中的值在每个单词后都有空格。 I am sure these blanks are null value or end of string indicator like "\\0"..Please tell me how to remove this before storing value in database. 我确定这些空格是空值或字符串指示符的结尾,例如“ \\ 0”。请在存储值到数据库之前告诉我如何删除它。

ps=con.prepareStatement("Insert into Daily_Entry_Details" + 
                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
for(int k=0;k<col;k++) {
    sdata=strdata[i][k].toString();
    ps.setString(k+1,sdata);
}
ps.executeUpdate();

this code is working fine but database values are shown with space at last. 该代码工作正常,但数据库值最后以空格显示。 this may be because of '\\0' .Please help me to remove this. 这可能是因为'\\ 0'所致。请帮助我删除它。 thnks 谢谢

It is possible ... but unlikely ... that you have NUL characters at the string. 字符串中有NUL个字符是可能的,但不太可能。 It is more likely that they are just spaces. 它们更有可能只是空间。

The way to get rid of leading and trailing whitespace is to use trim() ; 摆脱前导和尾随空格的方法是使用trim() eg 例如

    ps.setString(k+1, sdata.trim());

Note that in this context, "whitespace" means any character that is less or equal to SP ... and that includes NUL. 注意,在此上下文中,“空白”表示小于或等于SP ...且包括NUL的任何字符。

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

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