Hello people :)

Having some difficulties regarding a simple substring.
Then length of my line is 52 and I iterate it without any problems.

When I use this untill the 22nd character it is working

final String articleNumber = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 0, 22));

If I want to pass over the 22nd character, everything stops, empty.
Take a look at the StackTrace screenshot...can you figure it out why it is not working ?

488afabad01fca57105183078113085f

        final String articleNumber = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 0, 22));
        final String articleManufacturer = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 22, 4));
        final String descriptionNumber = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 26, 9));

Thank you!

That screenshot isn't a stacktrace, and doesn't help. You don't provide the source for your StringUtils class (t's not part of the standard Java API). That leaves us with more questons than answers.

You say "everything stops, empty." Do oyu mean the program terminates without any errors? What exactly is is that's "empty"?

My bad ...

This is the link commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#substring(java.lang.String, int, int)

for the Substring.

Tha application does not stop, it is built upon Spring and this is a file processor that reads all the lines.

As you can see on the first printscreen, the characters are there, but the method exits.

LE: with the standard value.substring() method, it works...

Did you mean "the method exits"?
If so there's a logic problem - you are assigning the substringed values to new variables in your method, which will cease to exist as soon as the method exits for any reason. Maybe we need to see the whole method?

Have you looked at the specification from the website?
[substring(click here)](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#substring(java.lang.String, int, int))

What are the actual parameters that the function takes?

Parameters:
str - the String to get the substring from, may be null
start - the position to start from, negative means count back from the end of the String by this many characters
end - the position to end at (exclusive), negative means count back from the end of the String by this many characters

What logic the end parameter presents?
What happens when you try do to something like this: StringUtils.substring(lineValue, 22, 4)?

Let's have an example:

    public static void main(String []args){
        String lineValue = gen();
        System.out.println(lineValue);
        System.out.println(StringUtils.substring(lineValue, 12, 6));
    }

    static String gen(){
        String genMe = "";
        while (genMe.length() < 54) genMe += Long.toHexString(Double.doubleToLongBits(Math.random()));
        return genMe.substring(0, 54);
    }

What will be output of the above code?

end - the position to end at (exclusive), negative means count back from the end of the String by this many characters

end is the actual end index in the string until it substrings (not included), not the length of the substring.
Knowing that, lets change somthing in the code, shall we? presuming that the you thought end is the length of the substring you need.

public static void main(String []args){
    String lineValue = gen();
    final String articleNumber = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 0, 22));
    final String articleManufacturer = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 22, 22 + 4));
    final String descriptionNumber = StringUtils.trimToEmpty(StringUtils.substring(lineValue, 26, 26 + 9));
    System.out.println(lineValue);
    System.out.println(articleNumber + "\n" + articleManufacturer + "\n" + descriptionNumber);
}

And to answer your question, you get an empty string becaue of this:

Gets a substring from the specified String avoiding exceptions.

Exception is thrown since you try to substring having as starting index 22 and as end index 4, and since StringUtils.substring avoids exceptions, it returns an empty string.

And as a friendly advice: Always carefully read the specification of libraries. :-)

commented: Well spotted! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.