st.replaceAll("\\s+","")
removes all whitespaces and non visible characters such as tab and newline. You can use this on the string you get from your JTextField
. This is a Regex.
Edit: the string.trim()
method is not what you want, because it trims the whitespaces from the start and end of the string, rather than getting rid of all of the whitespace.
Edit 2: If you want to compare strings, use string.equals(str2)
For example, if you have a license plate string called st = "AAA 111"
and another called st2 = "AAA111'
just strip them of whitespace like this st.replaceAll("\\s+","")
and st2.replaceAll("\\s+","")
and then you can check if they are equals by st.equals(st2)
.