I’m attempting to check if a string(filter) is contained in another string(formattedAmount) i.e. is filter a substring of formattedAmount.
I could not get it to work so I just changed the code to use “equals()” instead of “indexOf()”, purely for simplyfing the testing. The equals method does not appear to be working as I would expect either.
Here is a dummy script I wrote up that replicates what I am trying to do:
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
public class utils
{
public utils()
{
}
public static void main(String[] args) throws Exception
{
String filter = "333 333,44";
Number amount = new BigDecimal(333333.44);
NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRANCE);
nf.setMinimumFractionDigits(2);
String formattedAmount = nf.format(amount);
if (formattedAmount.equals(filter))
{
System.out.println("Working");
}
}
}
Any Ideas why it is not entering the If statement?
Thanks
A simple
printlnwill reveal the truth: the FRANCE locale thousands separator is NOT THE SPACE CHARACTER:Prints:
Hence, your two strings are not equal.
Try