简体   繁体   English

如何为字符串数组中的每个元素添加一个字符串前缀?

[英]How do I prefix a String to each element in an array of Strings?

I would like to know if there is, in Java, a function that can prefix a defined String to the beginning of every String of an array of Strings.我想知道在 Java 中是否有一个 function 可以将定义的字符串前缀到字符串数组的每个字符串的开头。

For example,例如,

my_function({"apple", "orange", "ant"}, "eat an ")  would return {"eat an apple", "eat an orange", "eat an ant"}

Currently, I coded this function, but I wonder if it already exists.目前,我编码了这个 function,但我想知道它是否已经存在。

Nope.没有。 Since it should be about a three-line function, you're probably better of just sticking with the one you coded.由于它应该是大约三行 function,因此您最好还是坚持使用您编写的那个。

Update更新

With Java 8, the syntax is simple enough I'm not even sure if it's worth creating a function for:使用 Java 8,语法很简单,我什至不确定是否值得为:

List<String> eatFoods = foodNames.stream()
    .map(s -> "eat an " + s)
    .collect(Collectors.toList());

Nothing like this exists in the java libraries. java 库中没有这样的东西。 This isn't Lisp, so Arrays are not Lists, and a bunch of List oriented functions aren't already provided for you.这不是 Lisp,所以 Arrays 不是列表,并且还没有为您提供一堆面向列表的功能。 That's partially due to Java's typing system, which would make it impractical to provide so many similar functions for all of the different types that can be used in a list-oriented manner.这部分是由于 Java 的类型系统,这使得为所有可以以面向列表的方式使用的不同类型提供如此多的类似函数是不切实际的。

public String[] prepend(String[] input, String prepend) {
   String[] output = new String[input.length];
   for (int index = 0; index < input.length; index++) {
      output[index] = "" + prepend + input[index];
   }
   return output;
}

Will do the trick for arrays, but there are also List interfaces, which include resizable ArrayList s, Vector s, Iteration s, LinkedList s, and on, and on, and on.可以为 arrays 解决问题,但也有List接口,其中包括可调整大小的ArrayListVectorIterationLinkedList等等。

Due to the particulars of object oriented programming, each one of these different implementations would have to implement "prepend(...)" which would put a heavy toll on anyone caring to implement a list of any kind.由于 object 面向编程的特殊性,这些不同的实现中的每一个都必须实现“prepend(...)”,这将对任何想要实现任何类型的列表的人造成沉重的负担。 In Lisp, this isn't so because the function can be stored independently of an Object.在 Lisp 中,情况并非如此,因为 function 可以独立于 Object 存储。

How about something like...怎么样...之类的

public static String[] appendTo(String toAppend, String... appendees) {
    for(int i=0;i<appendees.length;i++)
        appendees[i] = toAppend + appendees[i];
    return appendees;
}

String[] eating = appendTo("eat an ", "apple", "orange", "ant")

You could use a stream with a .concat() method reference您可以使用带有.concat()方法参考的 stream

    List<String> eatingFoods = foods.stream()
        .map("eat an "::concat)
        .collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据每个字符串的不同部分对字符串数组进行排序? - How do I sort an array of strings based on different parts of each string? 如何从Java中的字符串中删除字符串数组中除元素以外的所有内容? - how do I remove everything except an element from an array of strings from a string in java? 如何对 stream 字符串中的每个字符串进行排序? - How do I sort each string within a stream of strings? 如何测试字符串数组以查找特定字符串是否在数组中? - How do I test an array of strings to find if a specific string is in the array? 如何检查比较字符串值与数组中的每个元素? - How do you check to compare a string value to each element in an array? 在Spring中,如何在包(包括子包)中的每个bean id前面加上常量字符串? - How do I prefix each bean id in a package (inc. sub-packages) with a constant string in Spring? 如何在java中访问字符串数组的每个元素? - How can I access each element of a string array in java? 如何将这个数组的每个元素放入字符串中? - How can I put each element of this array into a string? 如何为数组中的每个元素创建一个计数器? - How do I create a counter for each element in an array? 如何将 2 个数组的第一个元素组合成一个字符串数组? - How do I combine the first element of 2 array together into a string array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM