Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Tuesday, 19 May 2015

How To Search String With in String In Java

The string class provide several methods to search a string for specified substring.it has two general methods 
Indexof() searches for the first occurrence of character or substring 
lastIndexOf() searches for the last occurence of a character or substring.
We can also specify a starting point from where we want to start searching using these forms 
int indexOf(String str,int startIndex)
int lastIndexOf(String str,int startIndex)

METHOD 1:

class index
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Pro java tricks is website of java tutorials");
int start;
int end;
System.out.println(sb);
start =sb.indexOf("java");
System.out.println("starting Index="+start);
end =sb.lastIndexOf("java");
System.out.println("Ending Index="+end);
}
}


OUTPUT:




METHOD 2:

There is another way to search Substring or character in a string by using String buffer.it has two general form 
String substring(int startIndex)
String substring(int startIndex,int endIndex)
First method is used when we only know the starting index of substring we want to search and second method is used when we know the starting and ending index of substring.


CODING:

class index
{
public static void main(String args[])
{
String sb="Pro java tricks is website of java tutorials";
int start;
int end;
System.out.println(sb);
System.out.println("index of java="+sb.indexOf("java"));
System.out.println("Lastindex of java="+sb.lastIndexOf("java"));
System.out.println("index of (java,10)="+sb.indexOf("java",10));
}

}


OUTPUT:













Thursday, 16 April 2015

,

How To Convert Array List To String Array

When working with array list sometimes we need to obtain data in string,int and in other data types for calculation and string processing but array list contains data in object not in int or string data types.so if want to obtain data in other types so first we need to convert array list data to desired data type in this program we have converted array list in a string array.In this program first we have created a of object type to store data temporary then we use toString method to convert it into a string then we initialize it into string array.


CODING:

>
import java.util.*;
class arraylist
{
public static void main(String args[])
{
ArrayList a1 = new ArrayList();
System.out.println("Initial size of a1 = "+a1.size());
a1.add("This");
a1.add("is");
a1.add("String");
Object Obj[]=a1.toArray();
String Str[]=new String[Obj.length];
for (int i=0;i<Str.length;i++)
{
Str[i]=Obj[i].toString();
System.out.println(Str[i]);
}
}
}

OUTPUT:




Tuesday, 24 March 2015

How To Reverse String In Java

There are many situation where we need to reverse the string there are many methods to do this but these methods are very complex to apply StringBuffer provides simple method its shown below
StringBuffer reverse()

CODING:

class reverse
{

public static void main(String args[])

{

StringBuffer sb = new StringBuffer("fedcba");

System.out.println("Before Reverse : "+sb);
sb.reverse();
System.out.println("After Reverse : "+sb);

}

}

OUTPUT:



Thursday, 19 February 2015

How To Replace Words In String

Another helpful method in StringBuffer  is replace() which is very useful when we want to replace some words in a string.It replace one set of character with another.Its signature shows below

StringBuffer replace(int startIndex,int endIndex,String str)

The String is to be  replaced is specified by starting and ending index.Thus substring at startIndex through endIndex-1 is replaced.

CODING:

class replace
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("This is test");
System.out.println("Original String:"+sb);
sb.replace(5,7,"was");

System.out.println("After Replace:"+sb);
}
}

OUTPUT:


Wednesday, 18 February 2015

How To Change Case Of String In Java

Some time we need to change input string into uppercase or into lower case fortunately java provides the simple way to do it  below code illustrate how to convert string into lower or upper case.

CODING:

class change
{
public static void main(String args[])
{
String s ="Pro Java Tuorial";
System.out.println("Original String:"+s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("In Upper Case:"+upper);
System.out.println("In Lower Case:"+lower);
}
}

OUTPUT:


Sunday, 15 February 2015

How To Sort Words Alphabetically In Java

This tutorial illustrate when to use compareToIgnoreCase in java in our last tutorial  we illustrate  How to sort string using compareTo  but in that tutorial words come first which are in upper case to ignore case transformation use this method  in this method words sort according to alphabetic order and ignore case.


CODING:

class ignore
{
public static void main(String args[])
{
String arr[] = {"e","d","f","b","a","F"};
System.out.println("Before sorting:");

for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j].compareToIgnoreCase(arr[i])<0) 
{
String t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}

OUTPUT:


Wednesday, 11 February 2015

How To Sort String In Java

This tutorial demonstrate how to sort string in java using compareTo() you can see from output "F" before all word because it is in the uppercase if you want to ignore case than use comareToIgnorecase()

compareTo Example in Java:

Often we need to know which strings are identical for sorting purpose to do this java has a method compareTo() this method compare two strings and returned  its result as shown below
less than zero = string is less than string
greatet than zero = string is greater than string
Zero = The string are equal

CODING:

class stringsorting
{
public static void main(String args[])
{
String arr[] = {"e","d","f","b","a","F"};
System.out.println("Before sorting:");

for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j].compareTo(arr[i])<0) 
{
String t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}

OUTPUT:




Sunday, 8 February 2015

How to extract words or cahracter form string in java

Fortunately in java the String class provide a number of ways in which characters can be extracted.Each is examined in this tutorial.

Use of charAt in Java:

To extract a single character from string we use charAt() its general form is
char charAt(int where)
where is the index of character that we want to obtain within the string 

Use of getChars() in java:

If we want to extract more than one character,than we use getschars() its general method is given below
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
source start and sourceend specify starting and ending index of desired substring respectively.while char target specify from which array character has to be extract following program demonstrate these two methods.

CODING:


class get
{
public static void main(String args[])
{
String s ="Pro java Tricks provide free tutorial";
int start=4;
int end = 8;
char buf[]= new char[end-start];
char ch;
ch=s.charAt(4);
s.getChars(start,end,buf,0);
System.out.print("Extracted word:");
System.out.print(buf);
System.out.println("Extracted Character:"+ch);

}
}


OUTPUT: