Java 在保留单词的同时修剪空格
我有一个数据文件,其中包含Java 在保留单词的同时修剪空格,java,string,Java,String,我有一个数据文件,其中包含字符串输入,如下所示: Tour g tour i stall 2 venue 2 我需要缩小空间以便进行适当的比较: for(int l=0; l<listOfFacilities.size(); l++) { String tempFacName = listOfFacilities.get(l).getFacilityName(); //string to be compared to if(tempFacName.contai
字符串
输入,如下所示:
Tour g
tour i
stall 2
venue 2
我需要缩小空间以便进行适当的比较:
for(int l=0; l<listOfFacilities.size(); l++)
{
String tempFacName = listOfFacilities.get(l).getFacilityName(); //string to be compared to
if(tempFacName.contains(facility.toLowerCase() )==true) //facility is the string from above taken from an imput file
System.out.println("true");
}
for(int l=0;l可以这样尝试:
tempFacName = tempFacName.replaceAll("\\s+", " ").trim();
这将用空格替换每个空格,然后删除字符串开头和结尾的空格
另请参见String.trim()