我有一个像控制台这样的小程序,它允许通过JTextField输入命令。
我有很多命令,输入被转换成字符串>,如果一个命令有像ping一个IP属性(ping 192.168.1.1)这样的子串5 (ping ),但现在当我想要像(command atr1 art22 atr333 art4444)这样的几个属性时,我会得到
String command = "command"; //I can make this one
String attribute1 = "atr1"; //I can make this one
String attribute2 = "atr22";
String attribute3 = "atr333";依此类推,但无论属性的长度是多少...因为我可以用子字符串来完成所有的事情,但是它会有长度定义!
发布于 2013-06-20 20:19:01
您可以使用String.split
String cmd = "command atr1 art22 atr333 art4444";
String[] parts = cmd.split(" ");split方法允许使用正则表达式。例如,如果空格的数量不同,这将非常有用:
String cmd = "command        atr1   art22 atr333     art4444";
String[] parts = cmd.split(" +"); // split by spans of one or more spaceshttps://stackoverflow.com/questions/17213450
复制相似问题