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...

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...