如何在Java中拆分字符串并在其间追加内容

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

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 = &quot;eaba848f6a9240f3ba5a5241a317ecc8&quot;;
    String value = &quot;extension_user&quot;;

    System.out.println(insertValues(value, &quot;_&quot;, code));
  }

  public static String insertValues(String str, String delim, String... values) {
    List&lt;String&gt; tokens = Stream.of(str.split(delim))
      .collect(Collectors.toCollection(ArrayList::new));
    return String.join(delim, insertValues(tokens, values));
  }

  public static &lt;E&gt; List&lt;E&gt; insertValues(List&lt;E&gt; list, E... values) {
    ListIterator&lt;E&gt; listIterator = list.listIterator();
    Iterator&lt;E&gt; 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 = &quot;extension_bla_bla&quot;; OR &quot;extension_bla&quot;;

String ID =&quot;whateveryourIDis&quot;;

String updatedValue = value.replace(&quot;extension_&quot;, &quot;extension_&quot; + ID + &quot;_&quot;);

return updatedValue;

This is your output &quot;extension_whateveryourIDis_bla_bla&quot;

huangapple
  • 本文由 发表于 2020年10月7日 21:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64245070.html
匿名

发表评论

匿名网友

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

确定