如何使用String对象解析Java中的输入 [英] How to use String object to parse input in Java
本文介绍了如何使用String对象解析Java中的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为实验,我正在用Java创建一个命令行实用程序,我需要解析用户的输入字符串.对于每次出现的&",都需要将字符串分成多个部分.使用Java中的String对象执行此操作的最佳方法是什么.
I'm creating a command line utility in Java as an experiment, and I need to parse a string of input from the user. The string needs to be separated into components for every occurrence of '&'. What's the best way to do this using the String object in Java.
这是我的基本代码:
//Process p = null;
Process p = null;
Runtime r = Runtime.getRuntime();
String textLine = "";
BufferedReader lineOfText = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("% ");
try {
textLine = lineOfText.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//System.out.println(textLine);
}
推荐答案
我认为最简单的方法是
String[] tokens = textLine.split("&");
这篇关于如何使用String对象解析Java中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文