Java 如何将字符串分为两部分,并将子字符串转移到不同的函数?
我是一个初学者,我正在编写一个程序,这将是一个简单的路由器模拟器 我想键入命令“ipA 192.0.0.1”(不同的IP),并想切换语句以检测“ipA”或“ipB”等。在分配给命令的每种情况下,我都想将IP(字符串)传输到其他函数。这是我的简单代码。有什么解决办法吗?它不起作用:(Java 如何将字符串分为两部分,并将子字符串转移到不同的函数?,java,string,function,switch-statement,substring,Java,String,Function,Switch Statement,Substring,我是一个初学者,我正在编写一个程序,这将是一个简单的路由器模拟器 我想键入命令“ipA 192.0.0.1”(不同的IP),并想切换语句以检测“ipA”或“ipB”等。在分配给命令的每种情况下,我都想将IP(字符串)传输到其他函数。这是我的简单代码。有什么解决办法吗?它不起作用:( }这应该让您开始: public static void main(String[] args) { com = new Commands(); scan = new Scanner(System
}这应该让您开始:
public static void main(String[] args) {
com = new Commands();
scan = new Scanner(System.in);
while (true) {
String o = scan.nextLine();
String s = o.split(" ")[0];
switch (s) {
case ("help"):
com.help();
break;
case ("ipA"):
com.ipA(o.substring(4));
break;
case ("ipB1"):
com.ipA(o.substring(5));
break;
case ("ipB2"):
com.ipA(o.substring(5));
break;
case ("ipC"):
com.ipA(o.substring(4));
break;
default:
System.out.println("Wrong!!!");
break;
}
}
}
在
case
中使用的表达式是布尔表达式。但是您的开关基于字符串,就像switch
一样。因此,您的案例也应该是字符串。这应该让您开始:
public static void main(String[] args) {
com = new Commands();
scan = new Scanner(System.in);
while (true) {
String o = scan.nextLine();
String s = o.split(" ")[0];
switch (s) {
case ("help"):
com.help();
break;
case ("ipA"):
com.ipA(o.substring(4));
break;
case ("ipB1"):
com.ipA(o.substring(5));
break;
case ("ipB2"):
com.ipA(o.substring(5));
break;
case ("ipC"):
com.ipA(o.substring(4));
break;
default:
System.out.println("Wrong!!!");
break;
}
}
}
在
case
中使用的表达式是布尔表达式。但是您的开关是基于字符串的,就像switch(s)
一样。因此您的案例也应该是字符串。这是if语句的解决方案。您可以使用if()
else if(){}
和最后使用else{}
覆盖所有可能的条件。将执行第一个true
结果,其余结果在运行时不会发生
package stackoverflow_Question39568263;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) {
final Scanner scan;
final Commands com;
com = new Commands();
scan = new Scanner(System.in);
while (true) {
final String s = scan.nextLine();
if (s == "help") {
com.help();
}
else if (s.substring(0, 3).equals("ipA")) {
com.ipA(s.substring(5));
}
else if (s.substring(0, 4).equals("ipB1")) {
com.ipA(s.substring(6));
}
else if (s.substring(0, 4).equals("ipB2")) {
com.ipA(s.substring(6));
}
else if (s.substring(0, 3).equals("ipC")) {
com.ipA(s.substring(5));
} else {
System.out.println("Wrong!!!");
}
}
}
}
这是if语句的解决方案。您可以使用
if()
else if(){}
并最终使用else{}
覆盖所有可能的条件。第一个true
结果将被执行,其余结果将在运行时不发生
package stackoverflow_Question39568263;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) {
final Scanner scan;
final Commands com;
com = new Commands();
scan = new Scanner(System.in);
while (true) {
final String s = scan.nextLine();
if (s == "help") {
com.help();
}
else if (s.substring(0, 3).equals("ipA")) {
com.ipA(s.substring(5));
}
else if (s.substring(0, 4).equals("ipB1")) {
com.ipA(s.substring(6));
}
else if (s.substring(0, 4).equals("ipB2")) {
com.ipA(s.substring(6));
}
else if (s.substring(0, 3).equals("ipC")) {
com.ipA(s.substring(5));
} else {
System.out.println("Wrong!!!");
}
}
}
}
您可以实现流式处理。主要好处是,您的处理代码在实现后不会改变,而不是在切换情况下: Class
Action
是您的操作是否适用于输入字符串的匹配器:
abstract class Action {
public static final Action WRONG_ACTION = new Action() {
@Override
public void apply(String s) {
System.out.println("Wrong");
}
};
private Pattern pattern;
public Action() {
}
public Action(String pattern) {
this.pattern = Pattern.compile(pattern);
}
public boolean isApplicable(String s) {
return pattern.matcher(s).matches();
}
abstract public void apply(String s);
}
处理方式如下所示:
private List<Action> actions;
actions.stream()
.filter(x -> x.isApplicable(s))
.findFirst()
.orElse(Action.WRONG_ACTION)
.apply(s);
您可以实现流式处理。主要好处是,您的处理代码在实现后不会改变,而不是在切换情况下: Class
Action
是您的操作是否适用于输入字符串的匹配器:
abstract class Action {
public static final Action WRONG_ACTION = new Action() {
@Override
public void apply(String s) {
System.out.println("Wrong");
}
};
private Pattern pattern;
public Action() {
}
public Action(String pattern) {
this.pattern = Pattern.compile(pattern);
}
public boolean isApplicable(String s) {
return pattern.matcher(s).matches();
}
abstract public void apply(String s);
}
处理方式如下所示:
private List<Action> actions;
actions.stream()
.filter(x -> x.isApplicable(s))
.findFirst()
.orElse(Action.WRONG_ACTION)
.apply(s);
你不能像在Java中那样使用
switch
。你可以用if
语句来代替。好的,我会用if语句来试试。谢谢!看看String
中的startsWith
。你不能像在Java中那样使用switch
。你可以用if
语句来代替。好的,我会用if语句来试试nks!查看字符串中的开始使用。