-1

I'm pretty new to Java, and I'm wondering if there is an easy way to splice a String[]. I'm using Java 11. As an example, I'm wanting to splice the args in main:

public static void main(String[] args) {
        // splice args
}

I know in Python this would just be args[2:], but I'm not sure about the equivalent here. I saw for other types you can do use the .copyOf(), but I saw in the Oracle docs there isn't a method for Strings.

Sumit Singh
  • 487
  • 5
  • 19
nskong
  • 19
  • 3
  • Strings are Objects, so `Arrays.copyOfRange` should work. – DelfikPro Jan 22 '21 at 03:14
  • You could just use a for-loop. ``String splice = args[2]; for (int i = 3; i < args.length(); i++) { splice += args[i]; }`` Do this only after making sure that args[] has a length of at least 3. – NomadMaker Jan 22 '21 at 03:32
  • 2
    Does this answer your question? [Get only part of an Array in Java?](https://stackoverflow.com/questions/11001720/get-only-part-of-an-array-in-java) – Omar Abdel Bari Jan 22 '21 at 04:15
  • What do you mean with splice? Please provide input and desired output, as most of the answers and comments here seem to think you mean slice. – Mark Rotteveel Jan 22 '21 at 14:07

2 Answers2

1

java does not have a splice, but it does have an Arrays::copyOfRange, which takes the array, the start and the end. So in your case:

Arrays.copyOfRange(args, 2, args.length) 
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

There has been a method static String.join(delimiter,string...) since Java 1.8.

    String[] a = {"one", "two", "three"};
    String b = String.join("", a);
    System.out.println(b);

Output

onetwothree
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190