Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在保留单词的同时修剪空格_Java_String - Fatal编程技术网

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()