Connecting Tech Pros Worldwide Forums | Help | Site Map

End of String Question

Newbie
 
Join Date: Mar 2008
Posts: 1
#1: Mar 4 '08
I have what I hope is a simple question but am seriously stuck...

Supposed to write a program that will ask the user for an address and then output some data...

I have part of the code working properly. Where I am stuck is that I need to tell the program to look at the last 5 characters in the string and output it...

Can anyone help? I know it has something to with length, indexof or substring but can't seem to figure out how to do it... any help is appreciated!!

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Mar 4 '08

re: End of String Question


Just think a bit; e.g. what does this do:

Expand|Select|Wrap|Line Numbers
  1. String s= "a test string";
  2. int n= 5;
  3. String q= s.substring(s.length()-n);
  4.  
Check all the methods in the API documentation for the String class.

kind regards,

Jos
Newbie
 
Join Date: Mar 2008
Posts: 6
#3: Mar 4 '08

re: End of String Question


Could you please send the code ?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Mar 4 '08

re: End of String Question


Quote:

Originally Posted by sujansarathi

Could you please send the code ?

You did read reply #2 didn't you?

kind regards,

Jos
Newbie
 
Join Date: Mar 2008
Posts: 6
#5: Mar 5 '08

re: End of String Question


Quote:

Originally Posted by JosAH

You did read reply #2 didn't you?

kind regards,

Jos


Sorry by mistake i sent it to u, its for the user who asked the question.
Newbie
 
Join Date: Mar 2008
Posts: 6
#6: Mar 5 '08

re: End of String Question


Quote:

Originally Posted by scoobyhcg

I have what I hope is a simple question but am seriously stuck...

Supposed to write a program that will ask the user for an address and then output some data...

I have part of the code working properly. Where I am stuck is that I need to tell the program to look at the last 5 characters in the string and output it...

Can anyone help? I know it has something to with length, indexof or substring but can't seem to figure out how to do it... any help is appreciated!!


Could you please send the code ?
Member
 
Join Date: Jul 2007
Location: Seymour,CT , Los Angeles, CA
Posts: 58
#7: Mar 5 '08

re: End of String Question


JosAH left a great hint for you...

Additionally, if you would like to be able to access any set of characters within the string, the Java String class also has a getChars() method that will allow you to convert the String input into an array of characters.

i.e. the string "Hello" gets broken into

char[] = {"H", "e","l","l","o"}

Then you can print out any particular character by accessing the
appropriate array index...

For your scenario, however, I would certainly look into JosAH's method...

Let us know if you're still having trouble,

Tom
Reply