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

Friday, 13 February 2015

WAP to find first Position of a String in another String


Hi , My today's post is on a program to find  Position of a String in another String.
For Example ,Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 6.



import java.util.Scanner;




public class SB

{




 public static void main(String[] args) {
 
  {
   String test=null;
   String index=null;
   int j=0;
   System.out.println("Enter the string");
   test= new Scanner(System.in).nextLine();
   System.out.println("Enter the substring you want to count");
   index= new Scanner(System.in).nextLine();
   for (int i = 0; i <= test.length()-index.length(); i++) {
    String testing=test.substring(i, index.length()+i);
   
    if(testing.equals(index))
    {
    j=i+1;
    System.out.println("It occurs first  at "+ j +" position" );
    break;
    }
   }

  }
 }
}
   
Read More »

Saturday, 6 September 2014

Difference between String ,String Builder and String Buffer



                                                           


String :

 String is immutable object i.e. once created cannot be changed.
object created is stored in Constant Spring pool.
Immutable objects means they can't be modified.
They are thread safe means can be used by only single thread at a time.

e.g. String value1="test";

this object is stored in constant pool and value cannot be modified.

How to reverse a string in java

StringBuffer :


StringBuffer is mutuable i.e. values can be changed.

It is defined as

String value1= new String ("test");

These values are stored in heap and can be modified at later point  of time

i.e. value1=new String ("testing");

now value1 is referring to an object which has value "testing".

 String Buffer is also synchronized means thread-safe.

Difference between Application Server and Web server 
String Builder :

String builder is also as same as String Buffer .It also stores value in heap.But unlike StringBuffer , String Builder is non synchronized which makes it working faster.


You might like :

Custom Exception in Java -Tutorial
Life Cycle of a thread 
How to reverse a string in java
Read More »