我有一个如下的列表
[url1,url2,url3,url4]
这个列表将基于HTML下拉列表的多选。因此,列表大小即列表元素会动态变化。我的问题是我无法获得在字符串中添加单引号的逻辑。 我希望上述字符串列表显示为:
'url1','url2','url3','url4'
每个字符串要用单引号 ('
),且逗号 (,
) 不能省略。请帮助我如何使用Java实现此功能。
你可以通过Java 8将列表进行转换。
list.stream().collect(Collectors.joining("','", "'", "'"));
import org.apache.commons.lang.StringUtils;
...
...
String arr[] = new String[4];
arr[0] = "my";
arr[1] = "name";
arr[2] = "is";
arr[3] = "baybora";
String join = "'" + StringUtils.join(arr,"','") + "'";
结果:
'my','name','is','baybora'
您可以使用Java 8实用程序类java.util.StringJoiner
。
这个类专门用于格式化字符串值列表。
例如:
StringJoiner joiner = new StringJoiner("','", "'", "'");
joiner.add("url1");
joiner.add("url2");
joiner.add("url3");
joiner.add("url4");
System.out.println(joiner);
输出
'url1','url2','url3','url4'
请注意,在StringJoiner
的构造函数中,
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
我们将单引号作为前缀和后缀传递以满足您的特定要求。
使用Java 8 Streams:
String[] array = {"url1", "url2", "url3", "url4"}
Stream.of(array).collect(Collectors.joining("','", "'", "'"));
ArrayList<String> a=[url1,url2,url3,url4];
String s ="'"+a.toString().replace("[","").replace("]", "").replace(" ","").replace(",","','")+"'";
遍历列表(使用for/while循环)。
对于列表中的每个元素,追加<element-of-list>
。提示:在StringBuilder
上使用append()
方法。
截取/子串列表以删除添加的最后一个元素。提示:在String
类上使用substring方法。
对于单引号
public static String singleQuote(String str) {
return (str != null ? "'" + str + "'" : null);
}
用于双引号
public static String doubleQuotes(String str) {
return (str != null ? "\"" + str + "\"" : null);
}
import java.util.ArrayList;
class Test1{
public static void main(String[] args) {
String[] s = {"url1","url2","url3","url4"};
ArrayList<String> sl = new ArrayList<String>();
for (int i = 0; i < s.length; i++) {
sl.add("'" + s[i] + "'");
}
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
@RunWith(MockitoJUnitRunner.class)
public class StringJoinerTest {
@Test
public void testStringJoiner() {
//init test data
String s1 = "team1";
String s2 = "team2";
List<String> teams = new ArrayList<>(2);
teams.add(s1);
teams.add(s2);
//configure StringJoiner
//when some values will be added to joiner it will return string started with prefix "(" an finished with suffix ")"
//between prefix and suffix values will be separated with delimiter ", "
StringJoiner teamNames = new StringJoiner(", ", "(", ")");
//if nothing has been added to the joiner it will return this value ('')
teamNames.setEmptyValue("(\'\')");
//fill joiner with data
for (String currentString : teams) {
//if you need to wrap each string in single quotes you can do this via org.apache.commons.lang3.StringUtils#wrap
// or org.springframework.util.StringUtils#quote
teamNames.add(StringUtils.quote(currentString));
}
System.out.println(teamNames.toString());
}
}
试试这个
String[] str = {"url1","url2","url3","url4"};
ArrayList<String> strList = new ArrayList<String>();
for (String k:str)
strList.add("'" +k+ "'");
System.out.println("Printing the list");
for(String k:strList)
System.out.println(k);
List<String>
(在这种情况下,它们将被显示在双引号之间),还是一个表示你的列表为"[url1, url2, etc]"的String
? - Mena