I have an ArrayList of strings that i need to modify. When i add them to the list they look like this.
tick.add("CAST.ST");
tick.add("ELUX-B.ST");
tick.add("EKTA-B.ST");
I then have a method to modify the names to remove “.ST” and add “.XML”
private static void addFileNames(List<String> ticks, List<File> fileNames)
{
List<String> tick = new ArrayList<String>();
for(int i=0; i<ticks.size(); i++)
{
tick.add(ticks.get(i).replaceAll(".ST", ".xml"));
}
for(int i=0; i<tick.size(); i++)
{
File f = new File("XML/" + tick.get(i));
fileNames.add(f);
}
This works perfectly for all Strings except “CAST.ST”, which gets modified to “C.xml.xml”.
I also tried replaceFirst and then it replaces the substring “AST” to “C.xml.ST”.
Can someone see what i am doing wrong here?
You need to escape the
.,\., as a period in regex means match any character. In the case of"CAST.ST"the posted regex will match"AST"and".ST".Change:
to: