Java 从远程服务解析错误的字符串
来自远程服务的任意字符串 根据Java 从远程服务解析错误的字符串,java,string,parsing,Java,String,Parsing,来自远程服务的任意字符串 根据主体:子字符串的存在,我必须以各种方式对其进行解析,我可以这样做: String longString = service.getString(); if (longString.indexOf("Body:") != -1) { // some code } else { // enother code } 但是当字符串进入并看起来像Body:\Dsdqwe….时,逻辑从else块运行。如何修复它
主体:
子字符串的存在,我必须以各种方式对其进行解析,我可以这样做:
String longString = service.getString();
if (longString.indexOf("Body:") != -1) {
// some code
} else {
// enother code
}
但是当字符串进入并看起来像Body:\Dsdqwe….
时,逻辑从else
块运行。如何修复它?改用这个-
字符串longString=service.getString()
我想你可以试试这个
String longString = service.getString().replaceAll("\","");
if (longString.indexOf("Body:") != -1) {
// some code
} else {
// enother code
}
这是一个有趣的问题 我创建了以下名为“strings.txt”的文本文件:
Hallo Welt
Body:
Body:Content
Body:\Dasdf
还有一个小方法,可以读取每个字符串并根据您的支票进行测试:
public void foobar()
{
// As the character \D is an invalid escape sequence, you can not hard code it without using double \.
// But using double \ would change the actual input as in the process, so read the data from a file.
List<String> stringList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("strings.txt"))) {
String line;
while ((line = br.readLine()) != null) {
stringList.add(line);
}
}
catch (Exception ex){
ex.printStackTrace();
}
// All strings are read from the file, now process and test each one of them
for(String str : stringList ) {
if (str.indexOf("Body:") != -1) {
System.out.println("'" + str + "' - Is containing the search term");
} else {
System.out.println("'" + str + "' - Is _NOT_ containing the search term");
}
}
}
结论:
如果您的程序接收到类似于Body:\Dasdf
的字符串,则可以正确地处理它,而不会出现任何问题。问题的根源一定在别的地方
以下是一些可能产生问题的想法:
- 编码:源以接收计算机以外的其他编码发送字符串。因此,接收方混淆了字符,字符串比较失败
- 编译:可能由于代码中的错误或IDE中的错误配置,您的源代码尚未编译
- 检查你的代码是否真的被编译过
- 你在使用序列化吗?如果是,请检查可能的编码问题
- 关于编码:不要相信你的IDE,它可能会为你转换东西。将原始接收到的字符串写入一个简单的文本文件,并使用文本编辑器(如记事本或记事本+)打开该文件以检查编码
String.contains
正在使用hood@DenisVabishchevich这不会解决你的问题,但Rishikesh有道理。毕竟,您不关心在字符串中找到它的位置,您只是在验证“Body:”是否在字符串中,使contains(String param)成为一个更好的选项,在if之前添加一个print语句来打印longString。如果字符串为“Body:\…”则不应转到else块。它是一个非常长的字符串
,但我只需要关注Body:
子字符串。也许\D
会造成问题?。它在else
block中打印Body:\Dsdqwe…
,通常不打印。索引应该是0,而不是-1。是否\D
字符可以删除前一个字符或破坏前一行?否。转义字符将转义后面的字符。它不会(不应该)影响前面的内容你是说“replaceAll”吗?最好是longString.replaceAll(“\”,“\”)
但它可能会使longString
@DenisVabishchevich上出现性能问题,但如果您尝试这样做:String test=“Body:\D”;它不会编译,所以很有可能,“\”把它搞砸了。虽然我希望有另一条错误消息,但仍然相当广泛,但从某种意义上说,这篇文章是一篇评论,而不是一篇回复。这篇文章对评论有点渴望,而且评论中的代码格式也不是很好。
public void foobar()
{
// As the character \D is an invalid escape sequence, you can not hard code it without using double \.
// But using double \ would change the actual input as in the process, so read the data from a file.
List<String> stringList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("strings.txt"))) {
String line;
while ((line = br.readLine()) != null) {
stringList.add(line);
}
}
catch (Exception ex){
ex.printStackTrace();
}
// All strings are read from the file, now process and test each one of them
for(String str : stringList ) {
if (str.indexOf("Body:") != -1) {
System.out.println("'" + str + "' - Is containing the search term");
} else {
System.out.println("'" + str + "' - Is _NOT_ containing the search term");
}
}
}
'Hallo Welt' - Is _NOT_ containing the search term
'Body:' - Is containing the search term
'Body:Content' - Is containing the search term
'Body:\Dasdf' - Is containing the search term