英文:
Take array element text before element containing specific text
问题
我根据文件夹根目录获取特定项目名称。使用以下代码:
private String getProjectName(Scenario scenario) {
List<String> pathArray = Arrays.asList(Path.of(scenario.getUri()).toUri().getPath().split("/"));
return pathArray.get(pathArray.size() - 1);
}
对于如下截图所示的结构,它可以正常工作。但是,如果在.feature文件之前有多个文件夹(编号9),则会失败。
我的想法是:
1)获取包含文本“features”的数组的索引。
2)移动到下一个索引(项目名称)并获取正确的文本,然后返回它。
这个想法好吗?
英文:
I'm getting specific project name based on the folder root. Using the code:
private String getProjectName(Scenario scenario) {
List<String> pathArray = Arrays.asList(Path.of(scenario.getUri()).toUri().getPath().split("/"));
return pathArray.get(pathArray.size() - 1);
}
For such structure as it's shown on below screenshot it works. However, when more then one folder before .feature (number 9) file name will be added then it will fail.
My idea is to:
- Get the index of an array which contains text: "features"
- Move to the next index (project name) and get the proper text + return it
Is it good idea?
答案1
得分: 1
你不知道你的文件夹结构有多深,因此需要一些模式来识别你知道会存在于项目结构中的元素,就像你的示例中的 "features" 文本一样,然后移动到下一个索引。所以你的想法对我来说是合理的。
英文:
As you do not know how nested your folder structure is, you need some pattern to recognize the element that you know will exist in the project structure, in your example with "features" text, and then move to the next index. So your idea looks fine to me.
答案2
得分: 1
我已经更新您的方法,以在“features”之后返回拆分值。
private String getProjectName(Scenario scenario) {
String desiredString = "features";
String[] pathArray = Path.of(scenario.getUri()).toUri().getPath().split("/");
for (int i = 0; i < pathArray.length; i++) {
if (pathArray[i].equals(desiredString)) {
return (pathArray[i + 1]);
}
}
throw new IllegalArgumentException(String.format("提供的URL不包含<%s>", desiredString));
}
英文:
I updated your method to return the split value after "features".
private String getProjectName(Scenario scenario) {
String desiredString = "features";
String[] pathArray = Path.of(scenario.getUri()).toUri().getPath().split("/");
for (int i = 0; i < pathArray.length; i++) {
if (pathArray[i].equals(desiredString)) {
return (pathArray[i + 1]);
}
}
throw new IllegalArgumentException(String.format("The url provided does not contain <%s>", desiredString));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论