Java 如何解析输入字符串并以规范化格式打印它?(爪哇)
我需要一个程序,将解析输入字符串(一个电子邮件地址),并以规范化格式打印它。它应该有3个部分,以“,”分隔,如下所示:Int,Int,String前两个整数部分表示程序应该提取的子字符串的第一个和最后一个字符的索引。 例如,假设我输入以下内容作为输入:0,6,alex。da@yahoo.com=>输出应为=>alex.da 有人知道怎么做吗Java 如何解析输入字符串并以规范化格式打印它?(爪哇),java,Java,我需要一个程序,将解析输入字符串(一个电子邮件地址),并以规范化格式打印它。它应该有3个部分,以“,”分隔,如下所示:Int,Int,String前两个整数部分表示程序应该提取的子字符串的第一个和最后一个字符的索引。 例如,假设我输入以下内容作为输入:0,6,alex。da@yahoo.com=>输出应为=>alex.da 有人知道怎么做吗 import java.util.Scanner; public class Labs02 { public static void main(String
import java.util.Scanner;
public class Labs02 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("enter your Email:");
String response = stdIn.nextLine();
String first=response.substring(0,1);
int second=response.indexOf(response.charAt(2));
System.out.println(response.substring(response.indexOf(response.substring(0, 1))+4));
} //This is where I get stuck!
使用
,
拆分整个字符串,然后解析开始的两个字符串以获得位置,并在第三个字符串上应用字符串#子字符串
,如下所示
String response = stdIn.nextLine();
String arr[] = response.split(","); // this will make three strings as "0" , "6", "alex.da@yahoo.com"
int first=Integer.parseInt(arr[0]);
int second=Integer.parseInt(arr[1]);
System.out.println(arr[2].substring(first, second + 1);
首先,您需要使用逗号作为分隔符,将输入字符串分隔为其构造部分:
List<String> paramaters = Arrays.asList(response.split(","));
变量首先会保存您的子字符串。删除了JavaScript标记。@sarah这种方法更安全,使用它非常感谢!我真的很感激。
get(int-index)
方法用于在列表中随机访问。我的错是,我只是在编辑器中键入,没有ide来指出这一点。但也会起作用。
String first = response.substring(Integer.parseInt(paramaters[0]),Integer.parseInt(parameters[1]));