Sunday, 19 August 2012

String In Java


Introduction of Java String


Java provide String objcect in which we directly store array of character.
Declaration of string object

String string-variable=new String('initial value');

or 

String string-variable='initial value';

String related functions

  1. length() this function determine length of string.
    Syntex :
    String str='initial value';
    
    str.length();
    
  2. toLowerCase() this function convert string to lower case
    Syntex :
    String str='initial value';
    
    str.toLowerCase();
    
  3. toUpperCase() this function convert string to upper case.
    Syntex :
    String str='initial value';
    
    str.toUpperCase();
    
  4. equals() This function check equality of two string. It consider case of both string.
    Syntex :
    String str='initial value';
    String str2='value2';
    str.equals(str2);
    
  5. equalsIgnoreCase() This function check equality of two string. It didn't consider case of both string.
    Syntex :
    String str='initial value';
    String str2='value2';
    str.equalsIgnoreCase(str2);
    
  6. indexOf() this function returns index of search string. This search from left to right and stops after first match string.
    Syntex :
    String str='initial value';
    
    str.indexOf('search value');
    
  7. indexIndexOf() this function returns index of search string. This search from right to left and stops after first match string.
    Syntex :
    String str='initial value';
    
    str.indexIndexOf('search value');
    
    
  8. substring() This function extract string with our specified positions.
    Syntex :
    String str='initial value';
    
    str.substring(string position,end position);
    
  9. trim() This function trims left and right blank spaces from string.
    Syntex :
    String str='  initial value  ';
    
    str.trim();
    
  10. replace() this function replace our search string with our new string.
    Syntex :
    String str='initial value';
    
    str.replace('search string','replace string');
    
Save This file StringDemo.java


public class StringDemo
{
	public static void main(String args[])
	{
		String s1 = "This is some sample string";
		String s2 = new String("This is some sample string");
		
		char aChar[] = {'a','b','c','d','e','f'};
		String s3 = new String(aChar);
		
		
		byte aByte[] = {65,66,67,68,69,70};
		String s4 = new String(aByte);
		
		String s5 ="This is some sample string";
		
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
		
		
		//To determine the no. of characters in the string.
		System.out.println("Length of string : " + s1.length());
		
		//to convert the string into lower case
		System.out.println(s1.toLowerCase());
		
		//to convert the string into upper case
		System.out.println(s1.toUpperCase());
		
		//Original string
		System.out.println(s1);
		
		
		aByte = s1.getBytes();
		int i;
		for (i=0;i<aByte.length;i++)
		{
			System.out.println(aByte[i]);
			aByte[i] = (byte)(aByte[i] + 10);
		}
			
		s5 = new String(aByte);
		
		System.out.println("Encrypted String : " + s5);
		
		aByte = s5.getBytes();
		for (i=0;i<aByte.length;i++)
			aByte[i] = (byte)(aByte[i] - 10);
			
		s5 = new String(aByte);
		System.out.println("Decrypted String : " + s5);
		
		
		
		//to compare the contents of two strings
		System.out.println(s1.equals(s2));
		
		s5 = "This is Some Sample String";
		System.out.println(s1.equals(s5));			
		System.out.println(s1.equalsIgnoreCase(s5));
		
		
		//to determine if a sub string exists within the string...
		s1.contains("AMP");
		s1.toLowerCase().contains("AMP".toLowerCase());
		
		
		System.out.println(s1.indexOf("is",3));
		
		
		s1 = "this is some sample string which is being used in this example. 
		it contains is multiple times";
		int position = -1;
		do
		{
			position = s1.indexOf("is",position+1);
			if (position != -1)
			{
				System.out.println("is found at index : " + position);
			}
		} while (position != -1);
		
		
		System.out.println(s1.lastIndexOf("is", 75));
		
		
		
		//to extract a part of string
		System.out.println(s1.substring(13,19));
		
		
		//to remove the leading and tralining spaces
		s1 = "    this    is    string    with    multiple    spaces    ";
		System.out.println("|" + s1.trim() + "|");
		
		
		//to break the string on the basis of some particular token we have
		s1 = "This is some sample string";
		String aWord[] = s1.split(" ");
		for (i=0;i<aWord.length;i++)
			System.out.println(aWord[i]);
			
			
		//to replace a substring within the main string
		System.out.println(s1.replace("s","SSSSSS"));	
			
		
	}
}

No comments:

Post a Comment