If your string is Welcome to Java, str.substring(5) will give you the string from index 5 up to the last ch so your output will be me to Java. Index starts from 0. Position and index are used in the same context(both start from 0: zeroth position or zeroth index means the same)
Also, str.substring(start index, end index) will return you the substring from start index to (end index -1) character. In our case str.substring(5,14) will return me to Jav
Videos
If your string is Welcome to Java, str.substring(5) will give you the string from index 5 up to the last ch so your output will be me to Java. Index starts from 0. Position and index are used in the same context(both start from 0: zeroth position or zeroth index means the same)
Also, str.substring(start index, end index) will return you the substring from start index to (end index -1) character. In our case str.substring(5,14) will return me to Jav
In java, the index start from 0. For the example you gave, let's say you created a string like this.
String s = "Welcome to Java";
So, this string has 15 characters in it. If you call below, it will return 15.
s.length()
If you draw a table, it should looks like this:
-------------------------------------------
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
------------------------------------------
character | W | e | l | c | o | m | e | |
-------------------------------------------
---------------------------------------
Index | 8 | 9 | 10| 11| 12| 13| 14|
---------------------------------------
character | t | o | | J | a | v | a |
---------------------------------------
At position 5, since the java start the index with 0, you now know that you have m at position (or index ) at 5, which is 6th character.
---------------
Index | 5 |
---------------
character | m |
---------------
Usually, in java, position and index means same. However, if the person (whoever asked this question to trick you) say "we use the term 'position 5,' (so he meant 5th character in the string as position 5), then you should start the substring at index of 4.
Yes. it is confused. However, when we say position 5, we usually mean the 6th character where index started at 0.
---------------
Index | 4 |
---------------
character | o |
---------------
Now, let's assume that the person (who asked this question) meant the position 5 as 5th character in the string. Then, you will need to think that, your start index to substring is 4.
So, if the person would like to substring from the nth character in a string (where n is a number between 1 to string length), then you want to start substring at n-1, because, as I said, the index starts at 0, in Java.
Now, let's look at the substring. Here is the documentation about the substring method: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Since you would like print a substring of given string, from index 4, you will call...
System.out.println(s.substring(4));
What is the output? "me to Java." ? I this case, the output should be
"ome to Java"
However, if you meant the position 5, same as index 5, then you will call...
System.out.println(s.substring(5));
Which will print
"me to Java"
I am provided a string. This string is a comma separated list of IDs. The list could have one ID, or it could have 1000 IDs (or any number of IDs). I need to determine how many characters the first ID is, whether it's alone or the first of many.
Currently I have a substring that uses indexOf as the end index, and uses the index of a comma. If there is no comma then the damn thing blows up. I have an OR clause that basically says "If this string up to the first comma is 5 digits OR the entire string is 5 digits, then do something".
I thought this would take care of the scenario with no comma. It doesn't. What is the best way to handle this?
EDIT I would share the code, but this is a massive application for work, and I'm guessing I'd get fired for sharing it, and according to the rules of this sub can't share code that isn't runnable. So while I REALLY think the one line I'm having issue with will help illustrate my problem very well, according to the sub rules I can't share it.
Use lastIndexOf. Also increase the initial offset to allow for the number of characters in the sub-string state(:
String state = myString.substring(myString.indexOf("state(") + 6, myString.lastIndexOf(")"));
You can use Regex.
Matcher matcher = Pattern.compile("(state)(\(.*?\))").matcher(text);
String state = matcher.group(2);