Java


Java Tutorial


Admission Enquiry Form

  

String and String Functions in Java




I. String Object in Java

public class StringClass
{
    public static void main(String[] args)
    {
        String str;
        str="Compuhelp";
        System.out.println(str);
    }
}

Output

Compuhelp



II. String Object in Java

public class StringClass
{
public static void main(String[] args)
{
String str="Compuhelp";
System.out.println(str);
}
}

Output

Compuhelp



III. String Object in Java

public class StringClass
{
public static void main(String[] args)
{
String str=new String();
str="Compuhelp";
System.out.println(str);
}
}

Output

Compuhelp



IV. String Object in Java Using Parameterized Constructor

public class StringClass
{
public static void main(String[] args)
{
String str=new String("Compuhelp");//use of constructor
System.out.println(str);
}
}

Output

Compuhelp



String Functions

str.length()

public class StringClass
{
public static void main(String[] args)
{
int len;
String str=new String("Compuhelp");
len=str.length();
System.out.println("Length of string is "+len);
}
}

Output

Length of string is 9



str.toUpperCase()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("compuhelp");
String str1=new String();
str1=str.toUpperCase();
System.out.println("String in Uppercase is "+str1);
}
}

Output

String in Uppercase is COMPUHELP



str.toLowerCase()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("COMPUHELP");
String str1=new String();
str1=str.toLowerCase();
System.out.println("String in Lowercase is "+str1);
}
}

Output

String in Lowercase is compuhelp



str.replace()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("computer");
String str1=new String();
str1=str.replace("ter","HELP");
System.out.println("String replacement is "+str1);
}
}

Output

String replacement is compuHELP



str.equals()

public class StringClass
{
public static void main(String[] args)
{
boolean bool;
String str=new String("compuhelp");
String str1=new String("COMPUHELP");
bool=str.equals(str1);
System.out.println(bool);
}

Output

false



str.equalsIgnoreCase()

public class StringClass
{
public static void main(String[] args)
{
boolean bool;
String str=new String("compuhelp");
String str1=new String("COMPUHELP");
bool=str.equalsIgnoreCase(str1);
System.out.println(bool);
}

Output

true



str.indexOf()

public class StringClass
{
public static void main(String[] args)
{
int index;
String str=new String("compuhelp");
index=str.indexOf('p');
System.out.println("Index of first 'p' is "+index);
index=str.indexOf("help");
System.out.println("Index of \"help\" is "+index);
}

Output

Index of first 'p' is 3
Index of "help" is 5



str.lastIndexOf()

public class StringClass
{
public static void main(String[] args)
{
int index;
String str=new String("compuhelp");
index=str.lastIndexOf('p');
System.out.println("Index of last 'p' is "+index);
index=str.lastIndexOf("help");
System.out.println("Index of \"help\" is "+index);
}

Output

Index of last 'p' is 8
Index of "help" is 5



str.charAt()

public class StringClass
{
public static void main(String[] args)
{
char ch;
String str=new String("compuhelp");
ch=str.charAt(4);
System.out.println("Character at location 4 is "+ch);
}
}

Output

Character at location 4 is u



str.substring()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("compuhelp");
String str1=new String();
str1=str.substring(3);
System.out.println("Substring is "+str1);
str1=str.substring(3,7);
System.out.println("Substring is "+str1);
}
}

Output

Substring is puhelp
Substring is puhe



str.concat()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("Welcome to ");
String str1=new String("Compuhelp");
String str2=new String();
str2=str.concat(str1);
System.out.println("Concatenated string is "+str2);

    }
}

Output

Concatenated string is Welcome to Compuhelp



str.trim()

public class StringClass
{
public static void main(String[] args)
{
String str=new String("   compuhelp      ");
String str1=new String();
str1=str.trim();
System.out.println("String after trim is "+str1);`
}
}

Output

String after trim is compuhelp



str.compareTo()

The compareTo() method does the comparison of two strings,which is based on the Unicode value of each character in the strings.

public class StringClass
{
public static void main(String[] args)
{
int n;
String str=new String("compuhelp");
String str1=new String("COMPUHELP");
n=str.compareTo(str1);
if(n==0)
{
System.out.println("Both strings are equal");
}
else if(n>0)
{
System.out.println("First string is greater than second string");
}
else if(n<0)
{
System.out.println("Second string is greater than first string");
}
}
}

Output

First string is greater than second string