0
public class sample {

public static void main(String[] args) {
    String[] test = new String[1024];
    int count = 0;
    test[count] = "33";
    count++;
    test[count] = "34";

    String s = new String();

This is just a simplified version, but I would like to append elements to a String variable s up to the index value of count without using StringBuilder, is there a way to do it? Thank you.

edit: without using loop as well, is there a String manipulation function I can use?

5
  • You want to append to s all test[0..count] values? Commented Dec 18, 2019 at 5:53
  • You could do in loop something like => for(int i=0; i<=count; i++) { s += test[i]; } Commented Dec 18, 2019 at 5:53
  • @ernest_k yes! means, my String s will be "3334"! Commented Dec 18, 2019 at 5:53
  • @Prakash13 sorry i forgot to mention without using loop as well! Commented Dec 18, 2019 at 5:54
  • 1
    @Ken You could make a method like everytime you increment count and insert in test[count]=something, also do s+=test[count] or s+=something Commented Dec 18, 2019 at 5:57

5 Answers 5

1

One way to do that is using String.join and Arrays.copyOf:

String s = String.join("", Arrays.copyOf(test, count + 1));

Which, with your test data, produces 3334

Sign up to request clarification or add additional context in comments.

Comments

0

Dont quite understand what you want...
But I guess you could user char array?

char[] c = new char[maxCount] 
for(int i = 0;i<maxCount;i++){
  c[i] = "34";
}

String s = String.valueOf(c)

Hope this could help you:)

Comments

0

Hard to say, what you're asking for...

Concatenating consecutive numbers could be easily done with a stream:

String s = IntStream.rangeClosed(0, 1024)
        .mapToObj(Integer::toString)
        .collect(Collectors.joining());

Comments

0

We can use the join function of String. Assuming the test is the string array.

String joinedString = String.join("", Arrays.stream(test).limit(count).collect(Collectors.toList()))

Comments

0

You can use String.join()

public class Main {

public static void main(String[] args) {
    String[] test = new String[1024];
    int count = 0;
    test[count] = "33";
    count++;
    test[count] = "34";

    String s = new String();
    System.out.println(s=String.join("", Arrays.copyOf(test, count + 1)));
} 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.