英文:
To remove last two words from a string
问题
我有以下示例字符串,
abc/def/ghi/test/yvw/syz
abc/fed/igh/test_123/wuvacv/zyxh
我想要得到以下形式,
abc/def/ghi/test
abc/fed/igh/test_123
那么在字符串操作中应该采用哪种最佳方法?
提前感谢。
我正在尝试通过"/"进行拆分,然后将单词放入数组中并循环,但卡在中间。在这里,我不能使用正则表达式,因为最后两个单词可以是任何内容。
英文:
I have sample strings like below,
abc/def/ghi/test/yvw/syz
abc/fed/igh/test_123/wuvacv/zyxh
I want to get it like below
abc/def/ghi/test
abc/fed/igh/test_123
So what are the best way to follow for string manipulation
Thanks in advance.
I'm trying to split by "/" and put the words into an array and looping it.but stuck in middle. Here I cant use regex here cas last two words can be anything.
答案1
得分: 2
你可以使用简单的正则表达式来匹配斜杠之间的任何文本:
str = str.replaceAll("/[^/]+/[^/]+$", "");
或者你可以像这样拆分和重新连接:
String[] parts = str.split("/");
str = String.join("/", Arrays.asList(parts).subList(0, parts.length - 2));
英文:
You can use a simple regex to match any text between the slashes:
str = str.replaceAll("/[^/]+/[^/]+$", "");
Or you can split and rejoin like this:
String[] parts = str.split("/");
str = String.join("/", Arrays.asList(parts).subList(0, parts.length - 2));
答案2
得分: 0
我会使用String.lastIndexOf(String, int)
来搜索倒数第二个 /
,然后取子字符串。例如:
String[] arr = {
"abc/def/ghi/test/yvw/syz",
"abc/fed/igh/test_123/wuvacv/zyxh"
};
for (String s : arr) {
int p = s.lastIndexOf("/", s.lastIndexOf("/") - 1);
System.out.println(s.substring(0, p));
}
输出(如请求所示):
abc/def/ghi/test
abc/fed/igh/test_123
英文:
I would use String.lastIndexOf(String, int)
to search for the second to last /
and then take the substring. Like
String[] arr = {
"abc/def/ghi/test/yvw/syz",
"abc/fed/igh/test_123/wuvacv/zyxh"
};
for (String s : arr) {
int p = s.lastIndexOf("/", s.lastIndexOf("/") - 1);
System.out.println(s.substring(0, p));
}
Outputs (as requested)
abc/def/ghi/test
abc/fed/igh/test_123
答案3
得分: 0
Sure, here is the translated code:
public static String removeLastTwoWords(String str) {
String[] words = StringUtils.split(str, '/');
return String.join("/", ArrayUtils.subarray(words, 0, words.length - 2));
}
In case you do not want to split a string, you could use lastIndexOf()
:
public static String removeLastTwoWords(String str) {
int last = str.lastIndexOf('/');
int preLast = str.lastIndexOf('/', last - 1);
return preLast > 0 ? str.substring(0, preLast) : "";
}
英文:
public static String removeLastTwoWords(String str) {
String[] words = StringUtils.split(str, '/');
return String.join("/", ArrayUtils.subarray(words, 0, words.length - 2));
}
In case you do not want to split a string, you could use lastIndexOf()
:
public static String removeLastTwoWords(String str) {
int last = str.lastIndexOf('/');
int preLast = str.lastIndexOf('/', last - 1);
return preLast > 0 ? str.substring(0, preLast) : "";
}
答案4
得分: 0
以下是翻译好的部分:
有几种方法可以采用。
请注意,String#split 参数 is 一个正则表达式模式。
要创建一个循环,以 String#split 为基础进行迭代,您可以使用以下方法。
当它达到倒数第二个元素时,它将停止迭代,使用固定条件 strings.length - 2。
String parse(String string) {
StringBuilder parsed = new StringBuilder();
String[] strings = string.split("/");
for (int index = 0; index < strings.length - 2; index++) {
if (index != 0) parsed.append("/");
parsed.append(strings[index]);
}
return parsed.toString();
}
输出
abc/def/ghi/test
abc/fed/igh/test_123
如果您希望提供起始和限制索引,可以实现额外的方法参数。
limit 参数将是包含的。
/** @param limit 包含 */
String parse(String string, int startIndex, int limit) {
StringBuilder parsed = new StringBuilder();
String[] strings = string.split("/");
for (int index = startIndex; index <= limit; index++) {
if (index != 0) parsed.append("/");
parsed.append(strings[index]);
}
return parsed.toString();
}
以下是一个示例输入和输出。
String stringA = "abc/def/ghi/test/yvw/syz";
String stringB = "abc/fed/igh/test_123/wuvacv/zyxh";
System.out.println(parse(stringA, 0, 3));
System.out.println(parse(stringB, 0, 3));
abc/def/ghi/test
abc/fed/igh/test_123
最后,如果您的 string 值遵循相同的模式,其中包含的值始终从索引 0 开始,并在倒数第一个斜杠字符上结束,您可以利用 String#substring 和 String#lastIndexOf 方法。
lastIndexOf 调用的第二个参数是一个 offset 索引。
所以,它定位了最后一个斜杠字符,然后使用返回的索引作为另一个调用的偏移量。
String string = "abc/def/ghi/test/yvw/syz";
int indexOf = string.lastIndexOf('/', string.lastIndexOf('/') - 1);
string = string.substring(0, indexOf);
abc/def/ghi/test
英文:
There are a few approaches you can take.
Keep in mind, the String#split parameter is a regular expression pattern.
To create a loop that iterates on a String#split you can use the following.
It will stop iterating when it reaches the second to last element—using a fixed condition of strings.length - 2.
String parse(String string) {
StringBuilder parsed = new StringBuilder();
String[] strings = string.split("/");
for (int index = 0; index < strings.length - 2; index++) {
if (index != 0) parsed.append("/");
parsed.append(strings[index]);
}
return parsed.toString();
}
Output
abc/def/ghi/test
abc/fed/igh/test_123
If you would prefer to provide a starting and limiting index, you can implement additional method parameters.
The limit argument will be inclusive.
/** @param limit inclusive */
String parse(String string, int startIndex, int limit) {
StringBuilder parsed = new StringBuilder();
String[] strings = string.split("/");
for (int index = startIndex; index <= limit; index++) {
if (index != 0) parsed.append("/");
parsed.append(strings[index]);
}
return parsed.toString();
}
Here is an example input and output.
String stringA = "abc/def/ghi/test/yvw/syz";
String stringB = "abc/fed/igh/test_123/wuvacv/zyxh";
System.out.println(parse(stringA, 0, 3));
System.out.println(parse(stringB, 0, 3));
abc/def/ghi/test
abc/fed/igh/test_123
Finally, if your string value follows the same pattern, in which the contained value always starts at index 0, and ends on the first-to-last solidus character, you can utilize the String#substring and String#lastIndexOf methods.
The second parameter of the lastIndexOf call, here, is an offset index.
So, it is locating the last solidus character, and then using the returned index as an offset of another call.
String string = "abc/def/ghi/test/yvw/syz";
int indexOf = string.lastIndexOf('/', string.lastIndexOf('/') - 1);
string = string.substring(0, indexOf);
abc/def/ghi/test
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论