This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Return Two Rightmost Characters  RSS feed

 
Ranch Hand
Posts: 1855
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help with code that will return the two rightmost characters of a string. And how to handle if string is "".
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Javadoc API documentation of class String (a link to the documentation is in my signature below). It contains a lot of useful methods to manipulate strings. There are for example methods to return the number of characters in a string and to get a substring out of a string. With those two methods it's easy to solve your problem.

Experiment with Java yourself, that way you'll learn it much quicker then when someone just gives you the answer.
 
Steve Dyke
Ranch Hand
Posts: 1855
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My challenge is this:

My String could be 854411-401(001),854411-403(001),

or 854411-401(001),854411-403(001,002,

I need to check and see if the last two characters are "),"

From the API functions I don't see an obvious solution. Thats why I have aquired the assistance of the experts.
 
Sheriff
Posts: 23649
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Steve Dyke:
I need to check and see if the last two characters are "),".

So you want to see if the string ends with "),"? And you didn't see any method in the String API for that?
 
Steve Dyke
Ranch Hand
Posts: 1855
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some more digging I came up with:

myString = myString.substring(myString.length()-2)

It seems to work fine.

I am used to developing in another language which had a function
Right(myString,2)
 
Paul Clapham
Sheriff
Posts: 23649
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that will give you the last two characters of the string, or throw an exception if the string has zero characters or one character. But if you just want to know if the string ends with ")," thenwill do fine. It behaves nicely on empty strings, too.

And I used to develop in that other language too. Not any more.
[ December 10, 2007: Message edited by: Paul Clapham ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!