Reputation: 65
I am new to programming and working on a Java String assignment. The question says:
Here is what I have done:
String phrase = new String ("This is a String test.");
String middle3 = new String ("tri"); //I want to print the middle 3 characters in "phrase" which I think is "tri".
middle3 = phrase.substring (9, 11); // this only prints out 1 letter instead of 3
System.out.println ("middle3: " + middle3);
Here is my output:
Original phrase: This is a String test.
Length of the phrase: 18 characters
middle3: S
I also think that there are 18 characters in the string "phrase", but if there isn't, please let me know. Thank you in advance for all your help!
Upvotes: 1
Views: 34128
Reputation: 7042
First off, there are 22 characters in phrase
.
"This is a String test."
^^^^^^^^^^^^^^^^^^^^^^ --> 22
Note that spaces () count towards the character count. Additionally,
String
has a method length()
which will give you this number.
String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22
From there, we can get the middle three characters by working off of the value phraseLength/2
. The middle three characters would start one before the middle position, and stop one afterwards. However, because the string(int, int) method takes the end index to be exclusive
, we should increase it by one.
String phrase = "This is a String test.";
int phraseLength = phrase.length(); //22
String middle3 = phrase.substring(phraseLength/2 - 1, phraseLength/2 + 2); // will have the middle 3 chars.
If the length of phrase
is odd, this will return it middle 3 chars. If the length of phrase
is even (as it is here), this will return the left-middle 3 chars. (For example, in 1,2,3,4
, I'm using left-middle to represent 2
and right-middle to represent 3
).
Another note, it's both unnecessary and bad practice to write new String("asdf")
. Simply use the string literal instead.
String phrase = new String ("This is a String test."); //Bad
String phrase = "This is a String test."; //Good
Upvotes: 2
Reputation: 8229
Think about how to retrieve the middle 3 characters without hardcoding the bounds of the substring()
method. You can use the length()
method in this regard. For example, the middle character of a string of odd length will always be at index str.length()/2
, while the two middle characters of a string of even length will always be at index str.length()/2
and (str.length()/2) - 1
. So the definition of the three middle characters will be up to you. But for our sakes, we'll just make the 3 middle characters at indexes (str.length()/2)-1
, str.length()/2
, and (str.length()/2)+1
. With this information you can modify this line of code you had before,
middle3 = phrase.substring (9, 11);
to
middle3 = phrase.substring (phrase.length()/2 - 1, phrase.length()/2 + 2);
As to why the original line of code you had before was returning only one letter, it has to do with the parameters of the substring
method. The first parameter is inclusive, but the second parameter isn't. Therefore, you were only retrieving characters from 9 to 10.
This is a String test.
^^^
9,10,11 (11 is not included)
The three characters I pointed to are at indices 9, 10, and 11 respectively. You only retrieved the chars ' ' and 'S', which together is just " S". That explains the one-letter output before.
Upvotes: 5
Reputation: 1187
you have an error in the begind index and the end index in the string, this is the correct code:
String phrase = new String("This is a String test.");
String middle3 = phrase.substring(11, 14);
System.out.println("middle3: " + middle3);
Upvotes: 2