我有两种类型的字符串
command1|Destination-IP|DestinationPort
Command2|Destination-IP|DestinationPort|SourceIP|SourcePort|message
我试图拆分String以获取像这样开始编码的变量,但不确定这是最好的方法
public String dstIp="";
public String dstPort="";
public String srcIp="";
public String scrPort="";
public String message="";
public String command="";
int first = sentence.indexOf ("|");
if (first > 0)
{
int second = sentence.indexOf("|", first + 1);
int third = sentence.indexOf("|", second + 1);
command = sentence.substring(0,first);
dstIp= sentence.substring(first+1,second);
dstPort= sentence.substring(second+1,third);
我可以这样继续吗?或者也许使用正则表达式?如果String是
command1|Destination-IP|DestinationPort
我收到一个错误,因为没有第三个 |
最好在此处按管道符号划分输入:
String[] tokens = sentence.split( "[|]" ); // or sentence.split( "\\|" )
然后通过检查检查令牌数量tokens.length
并采取相应措施。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句