我有两个字符串
AAA
AAA
AAA
和
BBB
BBB
BBB
我正在寻找的是一种方法String concatenateHorizontally(String left, String right, String separator, options...)
,我可以将这些字符串传递给并接收字符串
AAA BBB
AAA BBB
AAA BBB
我在Vim中做了很多(使用块状可视模式),但找不到允许在Java代码中执行此操作的库。我知道,对于简单的例子来说,这是微不足道的实现,但我希望有人已经以更通用的方式解决了这个问题(例如,有选择来决定是替换还是追加,插入固定位置或线端,处理不同的换行符,纠正Unicode行为等。)。
有人能指点我这样的库来横向连接文本吗?
答案 0 :(得分:2)
您可以按行返回并交替追加。
没有代码示例,因为" Easy Kill"。
编辑:
因为" Nice"
而实施String concatenateHorizontally(final String left, final String right) {
String br = "\n";
String[] lefts = left.split(br);
String[] rights = right.split(br);
if(lefts.length!=rights.length)
throw new UnsupportedOperationException("This is where my time becomes money");
StringBuilder sB = new StringBuilder();
for (int i = 0; i < lefts.length; i++) {
sB.append(lefts[i]);
sB.append(rights[i]);
sB.append(br);
}
return sB.toString();
}
答案 1 :(得分:0)
试试这个:
class m
{
public static void main(String[] args)
{
String s="AAA"+"\n"+
"AAA"+"\n"+
"AAA"+"\n";
String s1="BBB"+"\n"+
"BBB"+"\n"+
"BBB"+"\n";
String[] gets=s.split("\n");
String[] gets1=s1.split("\n");
String concate="";
for(int i=0;i<3;i++)
{
concate=concate+gets[i]+"\t"+gets1[i]+"\n";
}
System.out.print(concate);
}
}
输出:
AAA BBB
AAA BBB
AAA BBB
答案 2 :(得分:0)
public class Test
{
public static void main(String[] args)
{
System.out.println(concatenateHorizontally("AAA\nBBB\nCCC", "111\n222\n333", "\n", " "));
System.out.println(concatenateHorizontally("AAA\nBBB", "111\n222\n333", "\n", " "));
System.out.println(concatenateHorizontally("AAA\nBBB\nCCC", "111\n222", "\n", " "));
}
private static String concatenateHorizontally(final String left, final String right, String br, String concat)
{
{
String[] lefts = left.split(br);
String[] rights = right.split(br);
int max = Math.max(lefts.length, rights.length);
StringBuilder sB = new StringBuilder();
for (int i = 0; i < max; i++)
{
if (i < lefts.length)
sB.append(lefts[i]).append(concat);
if (i < rights.length)
sB.append(rights[i]);
sB.append(br);
}
return sB.toString();
}
}
}