取特定文本包含元素之前的数组元素文本。

huangapple go评论62阅读模式
英文:

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&lt;String&gt; pathArray = Arrays.asList(Path.of(scenario.getUri()).toUri().getPath().split(&quot;/&quot;));
    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:

  1. Get the index of an array which contains text: "features"
  2. 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 = &quot;features&quot;;
    String[] pathArray = Path.of(scenario.getUri()).toUri().getPath().split(&quot;/&quot;);
	for (int i = 0; i &lt; pathArray.length; i++) {
		if (pathArray[i].equals(desiredString)) {
			return (pathArray[i + 1]);
		}
	}

	throw new IllegalArgumentException(String.format(&quot;The url provided does not contain &lt;%s&gt;&quot;, desiredString));
}

huangapple
  • 本文由 发表于 2023年2月16日 19:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定