gpt4 book ai didi

java - java中从n个字符串用户输入中提取单词

转载 作者:行者123 更新时间:2023-12-02 01:27:31 25 4
gpt4 key购买 nike

我正在尝试提取字符串中 , 之后的单词。我必须从用户输入中提取 n 个字符串。我不知道该怎么做?我只是尝试了一个字符串。谁能告诉我如何对用户的 n 个字符串执行相同的操作?

     String str = "hi,34,55";
Pattern pattern = Pattern.compile(",(.*?),", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.print(matcher.group(1));
}
System.out.print(" "+str.substring(str.lastIndexOf(",") + 1));
//get the string like this and extract only the numbers after ","
int n=in.nextInt();
String [] s=new String[44];
for(int i=0;i<n;i++)
{
s[i]=in.nextLine();
}

如果 n 为 3 并且输入字符串为 "hi,1,2"、 "hiii,2,3"、 "ok,3,4"则输出将为 1 2 2 3 3 4 (即数字在“,”之后)

这可以正常工作,打印“34 55”。现在我们必须从用户那里获取 n 值并获取 n 个字符串,并提取输入字符串中“,”后面的单词。

最佳答案

如果您要获取一组字符串作为输入(带逗号)并拆分每个输入,请尝试以下操作:

String[] strArray = null;
String nArray[] = {"Hi1,1,2" ,"Hi3,3,4","Hi5,5,6"};

for(int j=0 ;j < nArray.length ;j++) {

strArray = str.split(",");
for(int i = 1 ;i< strArray.length ;i++) {
System.out.println(strArray[i]);
}
}

打印输出时从计数器 1 (i=1) 开始

关于java - java中从n个字符串用户输入中提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com