• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the class name without the full package

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a small query.

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Sairam";
Integer i = new Integer(1);
System.out.println(str.getClass().getName());
System.out.println(i.getClass().getName());

}

The below code returns the class name with the full package structure i.e

java.lang.String
java.lang.Integer

But if i need only the class name without the full package how do i do it?

Kindly help me out.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
split the name on a period and take the last element in the resulting string array.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Various options

1. String.split() - break up the package name and just use the last token
2. String.indexOf() - find the position of the last '.' and then use String.substring().
3. Get the name of the package (Class.getPackage().getName()) and then use the length of this with the String.substring() method.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:
3. Get the name of the package (Class.getPackage().getName()) and then use the length of this with the String.substring() method.



I'm not sure this will always work. I don't think every Class object has a Package object. Particularly not if loading with non-system ClassLoader.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:
2. String.indexOf() - find the position of the last '.' and then use String.substring().

Just a suggestion - String.lastIndexOf(".") will find the position of the last '.' in the String.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Van Tuyl:
Just a suggestion - String.lastIndexOf(".") will find the position of the last '.' in the String.



Must get myself a proofreader
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I am a fan of regular expressions, I would like to add...

4. RegEx -- Use regular expressions to extract it

And to promote its usage, I'll even produce the code...



Henry
[ April 25, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
RegEx -- Use regular expressions to extract it

And to promote its usage, I'll even produce the code...



Henry

[ April 25, 2006: Message edited by: Henry Wong ]

Since you are not really interested in matching anything before the last '.' anyway, this regex could be reduced even further to:
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Since you are not really interested in matching anything before the last '.' anyway, this regex could be reduced even further to:



Good point... I do seem to have a bad habit of matching everything before extracting.

Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use JDK 5, which has added Class.getSimpleName().
[ April 25, 2006: Message edited by: Jim Yingst ]
reply
    Bookmark Topic Watch Topic
  • New Topic