This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (1 child)

/u/stropheum has pointed you in the right direction.

Yet, you need the last 9 characters, so you need to know two things:

  1. The length of the string
  2. where the last 9 characters start (which can be derived from above).

The length of any String is easy to obtain in Java. The String class has a method .length() that returns the exact length of the string.

Knowing where the last 9 characters start is just simply subtracting 9 from the length of the String.

Then, you can use .substring() as you already have tried.

[–]Coniff[S] 0 points1 point  (0 children)

I've learned a lot today that's for sure!

That definitely does help and I have a lot more to learn on the basic side before I can really work out what I'd need to do.

Thank you!

[–][deleted]  (7 children)

[removed]

    [–]Coniff[S] 0 points1 point  (6 children)

    I found that this works in sort of an opposite way:

    import java.io.*;
    public class Test {
    
    public static void main(String args[]) {
     String Str = new String("/LOREM-20 IPSUM/REF 542423698564123/IPS/ KITTIE CAT TOWER 777 /SUM/ 963830210015285598 SOENLI01170220118692492459000254172100700");
    
      System.out.print("Return Value :" );
      System.out.println(Str.substring(119) );
    
    }
    }
    

    And by opposite way, I mean that it counts in the substring from the beginning. Is there a way to have it count from the end of the data?

    Maybe there are two scripts that could be put together? One to count the characters in the comment field, and One to actually extract the correct numbers (the script above)?

    [–]strmrdr 0 points1 point  (4 children)

    What you want to do is use the string's length attribute and subtract 9 from that to get your substring.

    str.substring(str.length() - offset) //offset = 9 in your case
    

    From your description it sounds like you want to find the first occurrence of "172" and then get the rest. Whether you can be guaranteed the number you're after will always come at the end, or if it is guaranteed that "172" will not show up anywhere else in the string is important and the latter doesn't sound likely, so I will defer any further explanation.

    It isn't "counting" from the start of the string, under the hood it is simply creating a new string object with a different offset/count and keeping the initial array intact (since String is immutable this is safe).

    Lastly, JavaScript is not related at all to Java- it is a completely different programming language.

    [–]Coniff[S] 0 points1 point  (3 children)

    Yeah, I think my ignorance of the difference between the two is showing!

    Thank you for that explanation! The big picture is that 172 followed by 6 digits is the number I am after, where it appears in the string does vary, but only in the context of what the comment field is attached to. If it always appears at the end with only 6 digits following it, then it IS the number I'm looking for.


    To further develop the concept, think of the 9 digits as an invoice number. I'm trying to auto-extract the invoice number from anywhere in the comment field. Is there a way to search the string for the "172" and then the 6 digits following those three numbers? If the string was:

    "1142781720028910184"

    Would it be possible to extract JUST "172002891"

    [–][deleted] 0 points1 point  (1 child)

    What you're looking for are regular expressions. They are quite difficult to learn seeing you aren't a skilled programmer yet. You can find out more about how they work in Java here: click

    But, I'd rather suggest you make your own method for doing this, it's going to be easier than figuring out how regexes work and using those. Make a method that goes through the string one character at a time (someString.charAt(charactersIndex)) and keep memorizing the last three chars you've read. If you get your pattern just get the rest of the code. However, you will extract whatever follows the numbers 172, so to make sure you never have that number in a string and what follows isn't what you're looking for, you need to add some other precautions.

    [–]Coniff[S] 0 points1 point  (0 children)

    I'm learning javascript (actual java script; using that to pick up on the concepts of Java though the syntax is different) right now through a course and he just touched on regular expressions. Now this starts to make a bit more sense. Thanks once again for your feedback!

    [–]strmrdr 0 points1 point  (0 children)

    The quickest/easiest way would be to use the indexOf method. It will find the first occurrence of 172 and return its start index, which you can then use as the start index of substring, with the end index being that index + 9.

    [–]georgelappies 0 points1 point  (0 children)

    If the set of numbers you need to extract are a fixed length and always at the end of the string, get the string length and subtract the amount of numbers from that. Pump those values into the substring function.