在Java中,将字符串拆分为多个部分的安全方法是什么?
让我澄清我要问的问题。我有一个正在开发的java程序,它通过名为JLine2的readline库从键盘获取输入。该库将整个行类型作为命令,而不是将其分解为空间分隔的命令和参数。我要寻找的是一种安全的方法来分解作为输入传递的字符串 我尝试过使用数组,但由于我处于概念的早期阶段,我还不知道我最大的命令将有多少个参数,因此使用预初始化数组我认为不起作用。我遇到的问题是当我检查数组中的空值时,或者当我检查某个特定命令或参数是否存在时。Java不断抛出数组索引超出范围之类的异常。因为数组实际上没有数组索引1的值,该值是数组索引0中命令的参数 所以,我要寻找的是一种方法,可以将字符串安全地拆分为多个部分,而不会让Java在发生和数组异常时对我大喊大叫 以下是我可以提供的非常精简的代码 ConfigShell.class在Java中,将字符串拆分为多个部分的安全方法是什么?,java,string,split,readline,Java,String,Split,Readline,让我澄清我要问的问题。我有一个正在开发的java程序,它通过名为JLine2的readline库从键盘获取输入。该库将整个行类型作为命令,而不是将其分解为空间分隔的命令和参数。我要寻找的是一种安全的方法来分解作为输入传递的字符串 我尝试过使用数组,但由于我处于概念的早期阶段,我还不知道我最大的命令将有多少个参数,因此使用预初始化数组我认为不起作用。我遇到的问题是当我检查数组中的空值时,或者当我检查某个特定命令或参数是否存在时。Java不断抛出数组索引超出范围之类的异常。因为数组实际上没有数组索引
package shell;
import java.io.IOException;
import configFS.ConfigFS;
import jline.console.ConsoleReader;
public class ConfigShell {
private ConfigFS config;
public ConfigShell() throws IOException {
config = new ConfigFS();
}
public void init() throws IOException {
ConsoleReader console = new ConsoleReader();
// When the program starts we want to be placed at / (root).
console.setPrompt(">> ");
// In this case an infinite loop is better than a loop based on whether line is equal to null.
// This allows line to be equal to null and still stay inside the shell.
while (true) {
String line = console.readLine();
if (line != null) {
// If pre-initialize the array I can check for null as a value for an array index.
// If I did this at time I needed the array and there were not enough index occupied the system would return an exception.
String[] cmdArgs = new String[4];
// We need to split up the incoming line because JLine2 does not do it for me.
// This allows me to evaluate the entire command piece by piece rather all at once.
cmdArgs = line.split("\\s+");
if (cmdArgs[0] != null && cmdArgs[0].equals("add")) {
if (cmdArgs[1] != null && cmdArgs[1].equals("server")) {
if (cmdArgs[2] != null) {
config.addServer(cmdArgs[2]);
System.out.println("Added server " + cmdArgs[2] + " to the configuration successfully.");
}
}
}
if (cmdArgs[0].equals("exit")) {
System.exit(0);
}
}
}
}
}
测试注意事项:My Start.class main方法调用上述文件中的init方法。您可以执行以下操作:
String cmdArgs = line.split("\\s+");
然后,在访问任何特定索引之前,请检查数组的大小,以便不获取ArrayIndexOutOfBoundException
大概是这样的:
if(cmdArgs.length>=2){
//It means you have at least 2 elements
//Now its safe to access cmdArgs[0] and cmdArgs[1]
}
如果您的所有问题都是存储可变数量的字符串,那么可以使用
ArrayList
object
将其声明为ArrayList as=new ArrayList()代码>
然后,当您从命令字符串中拆分某些内容时,只需使用add
方法:
as.add(您的字符串)代码>
如果需要检索ArrayList的特定元素,可以使用其get
方法:
as.get(0)代码>
对于每个
循环,您可以使用处理所有元素:
for(String str: as) {
println(str):
}
请在此处和此处查看。因为我认为您可以根据需要使用StringTokenizer
类及其方法
请参见下面的示例代码:
if(line!=null)
{
StringTokenizer st=new StringTokenizer(line);// by default it takes space as delimiter....you can use as required as second argument in constructor...
while(st.hasMoreTokens())
{
String token1=st.nextToken();
// do your stuffs here..........
// I don't know exactly about your required logic here......
/* if(token1.equals("add"))
{
String token2=st.nextToken();
if(token2.equals("server"))
{
String token3=st.nextToken();
config.addServer(token3);
System.out.println("Added server " + token3 + " to the configuration successfully.");
}
}
*/
}// while closing...
}// outer if closing...
或者正如PM 77-1所说,您可以使用ArrayList。但在我看来,LinkedList应该是一个更好的选择。String.split()
应该可以工作,除非海报需要处理带引号的字符串。考虑一个文件系统上的文件,它的名称可以有空格。如果命令是服务器C:\Program Files\apache\bin\apache
,在空格上拆分可能会导致问题。@EricJablow-Hmm,在您提到之前,我没有考虑过这种情况。基本命令结构将更像这样。“添加服务器”。服务器名称更像是一个描述性名称,而不是一个文件路径。但它仍然提出了如何处理名称中的空格的问题。空格在服务器名称中是有效字符吗?我不知道。这取决于节目。处理空格和带引号的字符串是一个普遍的困难,可能会影响任何基于拆分的解决方案,具体取决于问题域。大多数可配置的程序使用特定格式的配置文件;考虑<代码> Apache < /Cord>。我可能错了,但我认为StringTokenizer在java 7中被贬低了。这就是我所使用的。@索利尼丝是的,你是对的。谢谢你提供的有用信息。我在这儿买的。