在Java中从字符串中读取字符
我想从以下字符串中提取IP地址: 106.220.155.36--[29/Sep/2015:09:51:52-0400]“GET/tutorial/grammar/LL/images/llparsetable.png HTTP/1.1”200 14284 为此,我决定一次读取一个字符的字符串。我尝试了以下代码:在Java中从字符串中读取字符,java,string,char,Java,String,Char,我想从以下字符串中提取IP地址: 106.220.155.36--[29/Sep/2015:09:51:52-0400]“GET/tutorial/grammar/LL/images/llparsetable.png HTTP/1.1”200 14284 为此,我决定一次读取一个字符的字符串。我尝试了以下代码: public static String uniqueIP(String line){ String IP = ""; while(line.next(
public static String uniqueIP(String line){
String IP = "";
while(line.next() != " "){
IP = IP + line.next();
}
return IP;
}
但是,下一种方法不起作用。还有其他方法可以使用吗?如果您确定您的字符串结构将始终相同,您可以用“--”分隔它
什么是
line.next()
?一个字符串或多或少是一个字符数组。我猜你是从哪里得到next
方法的?请查看字符串Javadoc;如果您正在编写Java代码(显然),Javadoc是第一个寻求帮助的地方,而不是Stackoverflow
String s = "106.220.155.36 - - [29/Sep/2015:09:51:52 -0400] "GET /tutorial/grammar/LL/images/llparsetable.png HTTP/1.1" 200 14284"
String ip = s.split("- -")[0].trim();