如何从输入字符串到数组中提取括号括起来的字符串值? [关闭]

问题描述 投票:-1回答:2

用户输入的字符串为:

[you] <bodyType> <age category> [profession] <gender> [pregnant]

如何将方括号([])和尖括号(<>)中的String值提取到数组中?

java user-input
2个回答
0
投票

[Java字符串提供了split()方法,因此您可以使用特定的字符(例如,字符“,”)进行分割:

String input = readInput(); //read the string from input
String[] values = input.split(",");

参见here类似问题或阅读Oracle Reference documentation


0
投票

您的问题最好通过使用正则表达式来解决,例如

(?<=[\[<]).*?(?=[]>])

© www.soinside.com 2019 - 2024. All rights reserved.