简体   繁体   中英

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.

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. thnks

It is possible ... but unlikely ... that you have NUL characters at the string. It is more likely that they are just spaces.

The way to get rid of leading and trailing whitespace is to use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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