Saturday, 12 April 2014

Fixed How to split a String by space or whitespace

Fixed How to split a String by space or white space

/*
 *
 */
public class SplitByWhitespace {

    /**
     * @param args
        public static void main(String[] args) {
       
        String  str = "How to split a String by space";
        String[] splited = str.split("\\s+");
        for(String splitedValue : splited){
        System.out.println("splitedValue :"+splitedValue);
        }
    }
}
/* Result
 *
 * splitedValue :How
splitedValue :to
splitedValue :split
splitedValue :a
splitedValue :String
splitedValue :by
splitedValue :space
 */

fixed how to remove space in string in java

fixed how to remove space in string in java.
resently i have to remve from string here is exmape how to remove Whitespace from given string

/*******************************************************************************/
public class RemovingWhitespace {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        String st="2013 2014 2015 2016 3017 2018 2019 2120";
       
        String newString =st.replaceAll("\\s+","");
       
        System.out.println(newString);
    }

}

/*******************************************************************************/
/* Result is
 * 20132014201520163017201820192120
 */

Monday, 16 December 2013

count occurrences of character in string in java



String s = "...";
int counter = 0;

 for( int i=0; i<s.length(); i++ )
{

if( s.charAt(i) == '@' )
{
counter++;
 }

 }

ArrayList in java

List<String> someList = new ArrayList<String>()
 // add "monkey", "donkey", "skeleton key" to someList

 someList.add(monkey1);
 someList.add(donkey2);
 someList.add(skeleton key3);
 someList.add(tesrt4);
 for(String item : someList )
 {

 System.out.println(item);

 }

How to add item to arraylist in java

How to add item to arraylist in java

List<String> someList = new ArrayList<String>()
 // add "monkey", "donkey", "skeleton key" to someList
someList.add(monkey1);
someList.add(donkey2);
someList.add(skeleton key3);
someList.add(tesrt4);

for(String item : someList )
{

 System.out.println(item);

 }

iterate through list java 

iterate through list using for each in java

 iterate through list using for each in java

List<String> someList = new ArrayList<String>()
 // add "monkey", "donkey", "skeleton key" to someList
someList.add(monkey1);
someList.add(donkey2);
someList.add(skeleton key3);
someList.add(tesrt4);

for(String item : someList )
{

 System.out.println(item);

 }



add comment block to methods in Eclipse

How to add comment block to methods in Eclipse..


Source -> Generate element comment

 

/**

*

*comment

*

*/

public void tets(){

 

}