aspose file tools*
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
Author

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 ?

Thanks.
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Hi,

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(" ","");
System.out.println(a);
}


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

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40


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.

Henry

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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an API to minified a string ?