String SomeLongString = JavaAPIMethodFor (String[] 字符串, String 分隔符)
String SomeLongString = JavaAPIMethodFor (String[] strings, String delimiter);
或者这也可以工作:
String SomeLongString = JavaAPIMethodConvertingArrayWithDelimeter (String[] strings, char delimiter)
我想将字符串连接成一个更大的字符串,但这根本没有用。我知道我可以使用 Arrays.toString(Object someString) 将所有数据附加到字符串中,然后调整字符串,删除不需要的字符。但这并不是真正高效,建造一些东西只是为了重建它。因此,循环遍历 String[] 并在每个元素之间添加我的字符可能是可行的方法:
import static org.junit.Assert.*;
import org.junit.Test;
public class DelimetedString {
private String delimitedString(String [] test, String delimiter){
StringBuilder result = new StringBuilder();
int counter = 0;
if(test.length > counter){
result.append(test[counter++]);
while(counter < test.length){
result.append(delimiter);
result.append(test[counter++]);
}
}
return result.toString();
}
@Test
public void test() {
String [] test = new String[]
{"cat","dog","mice","cars","trucks","cups","I don't know", "anything!"};
String delimiter = " |...| ";
assertEquals("DelimitedString misformed",
"cat |...| dog |...| mice |...| cars |...| trucks "
+"|...| cups |...| I don't know |...| anything!",
delimitedString(test, delimiter));
}
}
我想要的是在使用分词器后将字符串组合在一起。我放弃了这个想法,因为它可能比它的价值更麻烦。我选择从较大的字符串中处理子字符串,我在“答案”中包含了该代码。
我要问的是 - java API 是否具有与 delimitedString 函数等效的函数?不少人的回答似乎是否定的。
String SomeLongString = JavaAPIMethodFor (String[] strings, String delimiter);
Or this could work as well:
String SomeLongString = JavaAPIMethodConvertingArrayWithDelimeter (String[] strings, char delimiter)
I wanted to join strings into a larger string, but this is simply useless. I am aware that I could append all the data into a string using Arrays.toString(Object someString) then adjust the string, removing unwanted characters. But that's not really efficient, building something, only to rebuild it. So looping through the String[] and adding my character[s] between each element is probably the way to go:
import static org.junit.Assert.*;
import org.junit.Test;
public class DelimetedString {
private String delimitedString(String [] test, String delimiter){
StringBuilder result = new StringBuilder();
int counter = 0;
if(test.length > counter){
result.append(test[counter++]);
while(counter < test.length){
result.append(delimiter);
result.append(test[counter++]);
}
}
return result.toString();
}
@Test
public void test() {
String [] test = new String[]
{"cat","dog","mice","cars","trucks","cups","I don't know", "anything!"};
String delimiter = " |...| ";
assertEquals("DelimitedString misformed",
"cat |...| dog |...| mice |...| cars |...| trucks "
+"|...| cups |...| I don't know |...| anything!",
delimitedString(test, delimiter));
}
}
What I wanted was something to put together a string after using a tokenizer. I abandoned that Idea, since it's probably more cumbersome then it's worth. I chose to address a Sub-Strings from within the larger String, I included the code for that, in an "answer".
What I was asking is - Does the java API have an equivalent function as the delimitedString function? The answer from several people seems to be no.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,没有内置方法。你可以做的是获取它的子字符串:
As far as I know, there isn't a built in method. What you can do is taking the substring of it:
这是我最终整理的一堂课。我想将文本文件分成块,然后使用行号通过 Servlet 发送块以获得相关数据。这是一个在我家的服务器上运行的应用程序,它允许我跨不同设备读取文本文件,并保留与文件相关的元数据。
我测试了:
Here is a class that I ended up throwing together. I want to break apart a text file into chunks, then send the chunks through a Servlet using the row number to obtain the relevant data. This is a app running on a server in my home that will allow me to read my text files across different devices, and keep meta-data related to a file.
I tested against: