It's not a secret anymore!
The moose likes Beginning Java and the fly likes an API to minified a string ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "an API to minified a string ?" Watch "an API to minified a string ?" New topic

an API to minified a string ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
where can find an API to minified a string or file, removing all empty space and \n ... , just like JQuery minified version ?

Prabhat Shankar

Joined: Oct 08, 2009
Posts: 27

I don't have idea of API of that type but you can create your own minified API.

example is below for removing all empty space :

public static void main(String args[]){
String a="sdsdsd dddd fdsfdf";
a=a.replace(" ","");

Prabhat Shankar Consumer Court,Consumer Court India,Consumer Complaints,Complaint India, Web Value
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
If you want to remove all whitespace, use String.replaceAll with a pattern like "\\s".
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
For Java libraries that solve the more general problem of reducing the size of JavaScript or HTML, try googling "java minify". There are several solutions out there. Personally I've never used any of them, so can't really recommend one over another. But as usual, Google is our friend.
Henry Wong

Joined: Sep 28, 2004
Posts: 20526

And minification is a bit more than just removing whitespaces. First, only the whitespace that don't effect the functionality of a program is removed -- as removing the whitesppace in a string to be printed does affect the program. Second, comments must also be removed -- as removing carriage returns will chain the comments otherwise. This can get complex if you take into account comments within comments (for commenting out code), and commented out code with quotes with whitespaces. etc. etc. etc.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

Basically the best way would be to read the entire program flow into a memory model, then re-output that model leaving out what you don't want. Note that I said best, not easiest

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: an API to minified a string ?
It's not a secret anymore!