Friday, February 14, 2020

Java String Methods programs

Image result for Strings methods in java replace,convert, concept

Java String Mothods programs


1.charAt()


public class A1
{
public static void main(String[] args)
{
String s ="This is Mysure";
String s1= charAt(6);
System.out.println(s1);
}
}


2.Strings Replace,  replace ()


public class StrReplace
{
    public static void main(String[] args)
    {
        StringBuffer a=new StringBuffer("C++ programming is fun");
        System.out.println(a.replace(0,3,"Java"));
       
       
    }
}


3.Strings Insert, insert()


public class StrInsert
{
    public static void main(String[] args)
    {
        StringBuffer a=new StringBuffer("Java programming is ");
        System.out.println(a.insert(20,"fun"));
   

    }
}


4. Strings comparison, compareTo()


import java.util.*;
public class Comp
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence : ");
String s1=sc.next();
String s2="Mysore";
int s3=s1.compareTo (s2);
System.out.println(s3);
}
}



5. Strings equal, equal ()



import java.util.*;
public class Equa
{
public static void main(String[] args)
{
String s1="Mysore";
String s2="Mysore";
Boolean s3=s2.equals(s1);
System.out.println(s3);
}
}


6.contains()


import java.util.*;
public class S9
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter data:");
String a=sc.next();
String s="My";
Boolean s1=s.contains(a);
System.out.println(s1);
}
}

7.Strings Lower case, toLowerCase()


import java.util.*;
public class Lcase
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Uppercase sentence : ");
String a=sc.next();
sc.close();
String a1=a.toLowerCase();
System.out.println(a1);
}
}

8.Strings Upper case, toUpperCase()


import java.util.*;
public class Ucase
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a lower case sentence : ");
String a=sc.next();
sc.close();
String a1=a.toUpperCase();
System.out.println(a1);
}
}


9.Strings reverse


import java.util.Scanner;
public class ContrString
{
public static void main(String[] args)
{
       
        String original,reverse=" ";
        Scanner in =new Scanner(System.in);
        System.out.println("Enter a string to  reverse");
         original=in.nextLine();
       
        int length=original.length();

        for (int i = length -1 ;i>=0;i--)
            reverse=reverse + original.charAt(i);

        System.out.println("Reverse of entered string is  :" +reverse);
    }
}









No comments:

Post a Comment