不规则的字符串 不规则的空格该怎么处理
例如:
String ="a 2006年12月22日 5:00 TYR%^DYGF 45654 tttt tryrty
qqq"; 通过什么样的方式,才能处理成String = "a 2006年12月22日 5:00 TYR%^DYGF 45654 tttt tryrty qqq";
例如:
String ="a 2006年12月22日 5:00 TYR%^DYGF 45654 tttt tryrty
qqq"; 通过什么样的方式,才能处理成String = "a 2006年12月22日 5:00 TYR%^DYGF 45654 tttt tryrty qqq";
例如:
String input = "a b c\n aaa";
String regex = " *\n*";
Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher(input).replaceAll("").toString());以上的回答的回答只是一个思路,如果您需要向了解regex,可以参考jdk-doc中的介绍
gooooooooooooooooooood luck
int j=0;
int z=0;
boolean b=false;
String s1;
StringBuffer sb=new StringBuffer(500);
// String s = "abc d efd h ijk l mn o pq r s t u v w x y z";
String s = res.trim();
System.out.println(s.length());
for(int i=0;i<s.length()-1;i++)
{
char c=s.charAt(i);
if(c!=' ')
{
if(j!=-1)
{
if(b) j=i;
b=false;
if(s.charAt(i+1)==' ')
{
if(i!=s.length()-2)
{
z=i;
s1=s.substring(j,z+1);
sb.append(s1);
sb.append("/");
} }
if(i==s.length()-2)
{
z=i+1;
s1=s.substring(j,z+1);
sb.append(s1);
}
}
}
if(c==' ')
{
j=-1;
if(s.charAt(i+1)!=' ')
{
j=i;
b=true;
}
if(i==s.length()-2)
{
j=i+1;
z=i+1;
s1=s.substring(j,z+1);
sb.append(s1);
}
}
}
System.out.println(sb.toString());
}*/
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;public class Demo { public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("your input file path"));
StringBuffer buffer = new StringBuffer();
Pattern pattern = Pattern.compile("[\\s[ ]]+");
do {
String line = reader.readLine();
if(null == line) break;
for(String s : pattern.split(line)) {
buffer.append(s);
buffer.append(' ');
}
} while (true);
System.out.println(buffer.toString());
}}