Java How to concatenate all char from String array to new string?
I am doing a method
public static String merge(String... s)
This is the input:
String a = merge("AM ","L","GEDS","ORATKRR","","R TRTE","IO","TGAUU");
System.out.println(a);
Expected Result:
ALGORITMER OG DATASTRUKTURER
I am trying to run a loop many times so that it picks up s [0] .charAt (index) and appends it to the string for output. The problem I am running into is that when I try to run a loop for s [1] .charAt (1) it is null, I want it not to get StringIndexOutOfBoundsException and instead continue to s [2] and added s [2]. char for string.
How should I do it?
source to share
You need to check the length of each string before trying to access its i-th character:
StringBuilder sb = new StringBuilder();
int index = 0;
boolean maxLengthReached = false;
while (!maxLengthReached) {
maxLengthReached = true;
for (String str : input) {
if (index < str.length) {
sb.append(str.charAt(index));
maxLengthReached = false;
}
}
index++;
}
return sb.toString();
To clarify, I use boolean maxLengthReached
to determine when the last character of the longest is added to the output String
. If, in a full iteration over everything String
in the input array, we don't find String
long enough to have charAt(index)
, we know we're done.
source to share
First you need a method to get the longest one String
, something like -
private static String getLongestString(String... arr) {
String str = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i].length() > str.length()) {
str = arr[i];
}
}
return str;
}
Then you can write the nested tag in merge()
, something like -
public static String merge(String... stringArray) {
StringBuilder sb = new StringBuilder();
int pos = 0;
int len = getLongestString(stringArray).length();
while (pos < len) {
for (String str : stringArray) {
if (str.length() > pos) {
sb.append(str.charAt(pos));
}
}
pos++;
}
return sb.toString();
}
Then you can call it -
public static void main(String[] args) {
String a = merge("AM ", "L", "GEDS", "ORATKRR", "", "R TRTE", "IO",
"TGAUU");
System.out.println(a);
}
Logout (requested) -
ALGORITMER OG DATASTRUKTURER
source to share
The following code does what you need. It works for any number of lines because it uses varargs (three dots) which allows any number of lines to be passed tomerge
Use getLongest
() to find the length of the longest string.
static int getLongest(String... strings) {
int len = 0;
for(String str : strings) {
if(str.length() > len) {
len = str.length();
}
}
return len;
}
Then you concatenate all the i-th character from each String
in StringBuilder
.
static String merge(String ...strings) {
int longest = getLongest(strings);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < longest; i++) {
for(String str : strings) {
if(i < str.length()) {
sb.append(str.charAt(i));
}
}
}
return sb.toString();
}
public static void main(String[] args) {
String a = merge("AM ","L","GEDS","ORATKRR","","R TRTE","IO","TGAUU");
System.out.println(a);
}
Output
ALGORITMER OG DATASTRUKTURER
source to share