Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a application where I have to deal with a custom variable aVar-

String aVar = price/barcode/area1/true  // varName/varType/varScope/tracable

There may be some other attribute added to aVar seperated by a ('/'). To get varName, varType, varScope etc I have to do the following things, please see the code below -

      String[] token = aVar.split("/");  

      String varName = token[0];
      String varType = token[1];
      String varScope = token[2];
      String traceable = token[3];

Here you can see the varName is taken from token[0] which is price, varType is taken from token[1] which is 'barcode' and so on. Here I am assuming - after splitting varName always be in token[0], varType always be in token[1] and so on. Now my question is that - Does the String array returned by the split() method always contain the String token in a order by which they are appeared (price-->barcode-->area1-->true)?

I have tested this several times with some few input and found the order maintained. But I am not sure will it be ALWAYS true for a VERY LONG string.

share|improve this question
    
YES it is an ordered array from the first to the last value of the source string. –  Jens Jan 28 at 7:07
1  
I don't see how that code even compiles - there's no overload for String.split accepting a char. Do you mean aVar.split("/")? –  Jon Skeet Jan 28 at 7:08
    
@Jon Skeet, great finding. It was a typo (which I have fixed just now) comes from the being accustomed to java script split( ) version for char. –  Razib Jan 28 at 7:55
1  
This is why it's useful to create a short but complete program, compile and run it, then copy and paste that into the question. It avoids problems like this. –  Jon Skeet Jan 28 at 8:17

4 Answers 4

up vote 1 down vote accepted

as per javadoc here:

The substrings in the array are in the order in which they occur in this string.

watch for the line The substrings in the array are in the order in which they occur in this string. in the link.

share|improve this answer

Does the String array returned by the split() method always contain the String token in a order by which they are appeared (price-->barcode-->area1-->true)?

In a word - yes. String.split maintains the order of the inputted string.

share|improve this answer

Assuming you're actually calling String.split(String), that method's documentation includes:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

That method's documentation includes:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

(Emphasis mine.)

So yes, they'll be returned in order, guaranteed.

share|improve this answer

As you can see in the javadoc the result has an order:

The substrings in the array are in the order in which they occur in this string

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.