英文:
How to split and a string and append something in between in Java
问题
我有一个字符串 extension_user 和一个代码 eaba848f6a9240f3ba5a5241a317ecc8
我正在编写一个Java代码,以附加 extension_eaba848f6a9240f3ba5a5241a317ecc8_user。
我正在使用正则表达式从字符串中提取出 extension_,然后将其与代码以及剩余部分连接在一起,还有其他方法吗?
public static void main(String []args){
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String value = "extension_user";
for (String retval: value.split("extension_")) {
System.out.println(retval);
}
}
英文:
I have a String extension_user and a code eaba848f6a9240f3ba5a5241a317ecc8
I am writing a java code to append extension_eaba848f6a9240f3ba5a5241a317ecc8_user.
I amn using regular expression to break extension_ from the string and append with code and rest of the part or is there any other way?
public static void main(String []args){
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String value = "extension_user";
for (String retval: value.split("extension_")) {
System.out.println(retval);
}
}
答案1
得分: 6
查看String.split()方法,你可能想要做类似这样的操作:
String[] array = extension_user.split("_");
return array[0] + "_" + code + "_" + array[1];
英文:
Check out String.split() method, you may want to do something like
String[] array = extension_user.split("_");
return array[0] + "_" + code + "_" + array[1];
答案2
得分: 2
public String getExtensionStr(){
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
return "extension_" + code + "_user";
}
这对你不起作用吗?
在阅读了您的评论之后
String extStr = "extension_user";
String[] sList = extStr.split("_");
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String finalStr = sList[0] + "_" + code + sList[1];
英文:
public String getExtensionStr(){
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
return "extension_" + code + "_user";
}
Won't this work for you?
After reading your comments
String extStr = "extension_user";
String[] sList = extStr.split("_");
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String finalStr = sList[0]+ "_" + code + sList[1];
答案3
得分: 2
这应该可以工作。您可以根据需要重新命名方法或变量。
String userString = "extension_user";
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String newString = insert(userString, code);
System.out.println(newString);
输出
extension_eaba848f6a9240f3ba5a5241a317ecc8_user
这个方法 - 只需使用 replace
public static String insert(String original, String code) {
return original.replace("_", "_" + code + "_");
}
您还可以使用 String#replaceFirst,但在这种情况下,第一个参数是一个正则表达式,在某些情况下可能具有特殊含义。
英文:
This should work. You can rename the methods or variables as you see fit.
String userString = "extension_user";
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String newString = insert(userString, code);
System.out.println(newString);
prints
extension_eaba848f6a9240f3ba5a5241a317ecc8_user
The method - just use replace
public static String insert(String original, String code) {
return original.replace("_", "_" + code + "_");
}
You could also use String#replaceFirst but in that case, the first argument is a regular expression which in some cases may have a special meaning.
答案4
得分: 2
这是一个动态字符串插入方法的示例代码:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StringInsert {
public static void main(String[] args) {
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String value = "extension_user";
System.out.println(insertValues(value, "_", code));
}
public static String insertValues(String str, String delim, String... values) {
List<String> tokens = Stream.of(str.split(delim))
.collect(Collectors.toCollection(ArrayList::new));
return String.join(delim, insertValues(tokens, values));
}
public static <E> List<E> insertValues(List<E> list, E... values) {
ListIterator<E> listIterator = list.listIterator();
Iterator<E> valueIterator = Stream.of(values).iterator();
while (listIterator.hasNext()) {
listIterator.next();
if (valueIterator.hasNext()) {
listIterator.add(valueIterator.next());
}
}
return list;
}
}
希望这对你有所帮助。
英文:
Here is a dynamic string insert method:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StringInsert {
public static void main(String[] args) {
String code = "eaba848f6a9240f3ba5a5241a317ecc8";
String value = "extension_user";
System.out.println(insertValues(value, "_", code));
}
public static String insertValues(String str, String delim, String... values) {
List<String> tokens = Stream.of(str.split(delim))
.collect(Collectors.toCollection(ArrayList::new));
return String.join(delim, insertValues(tokens, values));
}
public static <E> List<E> insertValues(List<E> list, E... values) {
ListIterator<E> listIterator = list.listIterator();
Iterator<E> valueIterator = Stream.of(values).iterator();
while (listIterator.hasNext()) {
listIterator.next();
if (valueIterator.hasNext()) {
listIterator.add(valueIterator.next());
}
}
return list;
}
}
答案5
得分: 1
以下是更好的一行代码方法:
String value = "extension_bla_bla"; 或者 "extension_bla";
String ID = "whateveryourIDis";
String updatedValue = value.replace("extension_", "extension_" + ID + "_");
return updatedValue;
这是你的输出 "extension_whateveryourIDis_bla_bla"。
英文:
Here is a better one line approach,
String value = "extension_bla_bla"; OR "extension_bla";
String ID ="whateveryourIDis";
String updatedValue = value.replace("extension_", "extension_" + ID + "_");
return updatedValue;
This is your output "extension_whateveryourIDis_bla_bla"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论