java string explode and implode

Some nice java function to explode string into array or implode array into string as equivalent to PHP implode and explode function. Explode Convert string to array of tokens. Function #1 – Explode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //explode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String[] explodeStringUsingStringUtils(String stringToExplode,String separator){ return StringUtils.splitPreserveAllTokens(stringToExplode, separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingStringUtils(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Function #2 – Explode using core java.lang //explode function #2 //using core java lang public static String[] explodeStringUsingCoreJava(String stringToExplode,String separator){ return stringToExplode.split(separator); } ... //usage String stringToExplode = "apple,pear,banana,,durian"; String separator = ","; String[] arrExploded = explodeStringUsingCoreJava(stringToExplode,separator); /* * String[] arrExploded will contain * 0 - "apple" * 1 - "pear" * 2 - "banana" * 3 - "" * 4 - "durian" */ Implode Concatenate/merge array of string into a single continuous string. Function #1 – Implode using org.apache.commons.lang.StringUtils import org.apache.commons.lang3.StringUtils; ... //implode function #1 //using org.apache.commons.lang package //api and jar download available @ http://commons.apache.org/ public static String implodeArrayUsingStringUtils(String[] arrayToImplode,String separator){ return StringUtils.join(arrayToImplode, separator); } ... //usage String[] arrExploded = {"Denny","de","Web","Monster"}; String implodeString = implodeArrayUsingStringUtils(arrExploded,"+"); /* * String implodeString will contain * "Denny+de+Web+Monster" */ Function #2 – Implode using for loop //implode function #2 //using for loop public static String implodeArrayUsingForLoop(String[] arrayToImplode,String separator){ if (arrayToImplode.length == 0) { //empty array return empty string return ""; } if(arrayToImplode.length < 2){ //only 1 item return arrayToImplode[0]; } StringBuffer...

Prevent / Solve java.util.ConcurrentModificationException

Your program throws ConcurrentModificationException while running and how can you prevent / solve it It happens when your program removing an item in an ArrayList, Collection or List while looping (iterating) over it. Example: for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { yourArrayList.remove(i); } } //Exception in thread "main" java.util.ConcurrentModificationException // at java.util.AbstractList$Itr.checkForComodification(Unknown Source) // at java.util.AbstractList$Itr.next(Unknown Source) To prevent this, there are several solutions depending on you program logic. Solution 1 – Using the java.util.Iterator: import java.util.Iterator; ... Iterator<String> iterator = yourArrayList.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); if (string.compareToIgnoreCase("apple") == 0) { iterator.remove(); } } Solution 2 – Using an temporary ArrayList to store items for removal: //Initialize an temporary array list with similar type ArrayList<String> itemToRemove = new ArrayList<String>(); //Search for items matching string "apple" for (int i = 0; i < yourArrayList.size(); i++) { if (yourArrayList.get(i).compareToIgnoreCase("apple") == 0) { itemToRemove.add(yourArrayList.get(i)); } } //Remove items stored in itemToRemove ArrayList yourArrayList.removeAll(itemToRemove); Hope it...

Integrated Development Environment (IDE) for PHP / HTML / CSS / JavaScript

1. Aptana Studio 3 by Appcelerator, Inc Platform independent as the IDE runs on Java JRE and supported programming languages are HTML5, CSS3, JavaScript, Ruby, Rails, PHP and Python. Aptana Studio 3 is currently release under “GNU General Public License“.   2. Adobe Dreamweaver CS5.5 by Adobe Systems Favourite by large web development firms. Runs on mac osx and windows platform. Supported programming languages are HTML5, CSS3, JavaScript, PHP, ColdFusion Markup Language (CFML) , VBScript (for ASP), C# and Visual Basic (for ASP.NET) and JSP. Commercial license required to install and use.   3. Eclipse by Eclipse Foundation Open source community, whose projects are focused on building an open development platform comprised of extensible frameworks, tools and runtimes for building, deploying and managing software across the lifecycle. Platform independent as the IDE runs on Java JRE. Download the Eclipse PDT package for PHP support. Currently release under “Eclipse Public License (EPL)“.   4. Zend Studio 8 by Zend Technologies Ltd According to their FAQ, Zend Studio is a commercial IDE with rich a feature set including remote debugging, Zend Server integration, Zend Framework support, and more. Supported programming languages are HTML, CSS, JavaScript and PHP. If your presentation uses JavaScript libraries such as Dojo, jQuery, Prototype and Ext JS, then you may want to consider Zend Studio as it comes with content assist for the libraries. Zend Studio 8 runs on linux, mac osx and windows. Commercial license required for use after 30 days...

Add Google +1 Button To Your Site

Something similar to facebook like button. However +1 is also shown in google search result. Far more effective than other social plugins as it greatly extends your site exposure. “How do I implement the +1 button?” If you are using wordpress, download the google +1 social share button plugin for wordpress. Otherwise you may grab the code from google. There are also instructions on where to paste the 2 lines of code in your web...

PHP XMPP Google Talk send notification message to IPhone

The example below, I am going to show you how you can send notifications using PHP XMPP library. I am using CodeIgniter 2.0, a php framework, PHP XMPP library and MySQL database in the example. The receiving end, I am using an iPhone installed with an instant messaging app, setup with Google Talk account to receive push notifications. I chose IM+ free version. It’s up to you which app you prefer but I recommend at least one with push notifications on new messages so it served as some sort of “notification”. What you need? Latest version of PHP XMPP library. 2 x Google talk account, one for receiving and one for sending the notifications. PHP enabled server. Optional – MySql database for storing of notification queues. You may choose to read the notifications queue from a file, memory pipe and etc…. PHP server settings Important! Ensure PHP OpenSSL module is enabled. I encountered infinite loop when connecting to GTalk server while OpenSSL module is disabled. Click here if you need help. Firstly, install the PHP XMPP package in “/libraries/xmpp”. Create the model which reads the notification queue for new message to send. //M_notification_queue.php file ... function getUnsendNotifications(){ //Load the database $this->load->database(); //Build the sql query //Retrieve unsent messages only $this->db->where('status','unsent'); //Retrieve from table notification $query = $this->db->get('notification'); //return the active record handler return $query; } ... Implement the sending controller //notification.php controller file private $gtalk_username = "sender@gmail.com"; //fake email private $gtalk_password = "password"; //fake password private $check_notification_interval = 10; //frequency to check for unsent message ... function sendNotificationCronJob(){ //load required models $this->load->model('M_notification'); //initialize the xmpp library $conn = new...