Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Friday, 4 July 2014

How do I compare strings in Java

Use equals () to compare

two string 



package com.vipul;

public class StringComporation {

      /**
      * How do I compare strings in Java
      * @param args
      */
      public static void main(String[] args) {
            String fooString1 = new String("foo");
            String fooString2 = new String("foo");
            // Evaluates to true
            if(fooString1.equals(fooString2));
            {
                  System.out.println(fooString1);
            }
           

      }

}
==========================================================================
Maven + eclipse related 
An internal error occurred during: "Importing Maven projects". Unsupported IClasspathEntry kind=4 fixed

==================================================================================

How to convert StringBuilder to String

package com.vipul;

public class StringBuildertoString {

      /**
      * How to convert StringBuilder to String
      * @param args
      */
      public static void main(String[] args) {
            StringBuilder sb = new StringBuilder();
                sb.append("test");

                System.out.println(sb.toString());

      }

}

==========================================================================
Maven + eclipse related 
An internal error occurred during: "Importing Maven projects". Unsupported IClasspathEntry kind=4 fixed

==================================================================================

How to remove the last character in StringBuilder

How to remove the last character in StringBuilder


package com.vipul;

public class StringBufferDelete {

      /**
      * How to remove the last character in StringBuilder
      * @param args
      */
      public static void main(String[] args) {
            String text2 = "Test";         
              StringBuffer text=new StringBuffer(text2);
           // ...
           final int length = text.length();
           if ( length > 0 ) {
                // We have remove the last character.
                text.deleteCharAt( length - 1 );
           }
           System.out.println(text);
      }

}

==========================================================================
Maven + eclipse related 
An internal error occurred during: "Importing Maven projects". Unsupported IClasspathEntry kind=4 fixed

==================================================================================

convert string to ascii value

this is example of StringToAscii convertion in java.

string get character java ascii to value

package com.vipul;

public class StringToAscii {
      public static void main(String[] args) {

            char character = 'a';
            int ascii = (int) character;
            System.out.println("ascii is"+ascii);
      }
}


Output is ::
================================
ascii is:97
================================

Saturday, 12 April 2014

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++;
 }

 }

Tuesday, 10 December 2013

string pattern matched in java

string pattern in java

public class RegexMatches {
public static void main( String args[] ){
 // String to be scanned to find the pattern.
String line = "xxxx123@gmail.com";
String pattern = "@gmail.com";

if(line.endwith(pattern ))
{
 sysout("Matched");

     }else{
 sysout("Not Matched");
}
}
}

Monday, 9 December 2013

string is null or empty check in java

string null check in java

If we want to check weather string is empty or not then use the

String  str ="null";

if(str != null && !str.isEmpty())
{
 sysout("null");
}
else{
 sysout("not null");

}

Sunday, 17 November 2013

Split string using regular expression in java

How Split string using regular expression in java
 
public class RegEX {

      
       public static void main(String[] args) {
             String pattern = "([\\s+])";          
              String test="Have Nice Day ";
                 for(String token : test.split(pattern))
                     System.out.println(token);
                
       }

}

======================================
Result :
 
Have 
Nice
Day
======================================

Friday, 18 October 2013

string split by space in java

How to split a String by space

str = "Hello I'm your String Hello";

String[] splited = str.split("\\s+");

string-pattern-matched-in-java

string comparison in java

insted of "==" using for comparing String use always equals() method.

Easiest way to convert String to StringBuffer

 Easiest way to convert String to StringBuffer


split by space in java

split by space in java

str.split("\\s+");

This will split by space.