• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the class name without the full package  RSS feed

 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split the name on a period and take the last element in the resulting string array.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use JDK 5, which has added Class.getSimpleName().
[ April 25, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!