1.10 String in java.

String in Java :

-> String Function :

    String s1;
    s1 = "JavaStudy"; (set of character or array of character.)


s1        
 j a v a s t u d y
        0            1                2            3                4            5              6               7            8

            System.out.println('s1'); // 9

Function of String :

        eg:
        String s1 = 'javastudy';

 s1        
 j a v a s t u d y
        0            1                2            3                4            5              6               7            8

        1. length() : return number of character present in given string return type is int.
        eg :
            int x = s1.length();
            System.out.println(x);
            output : 9

        2. charAt(index) : return char present @ specified index position.
            eg:
                char c1 = s1.charAt(4);
                System.out.println(c1);
                output : s
    
        3. indexOf(char) : return first occurance position of  give char , return type is int.
            eg:
                int x2 = s1.indexOf(t);
                Syste.out.println(x2);
                output : 5

        4. lastIndexOf(char) : return last occurance position of given char return type int.
            eg :
                int x3 = s1.lastIndexOf('a');
                System.out.println(x3)
                output : 3

        5. contains(charSeq) : return true if given seq of char is present in string anywhere otherwise return             false.
            eg :
                boolean b1 = s1.contains("stu");
                System.out.println(b1);
                output : true

        6. startsWith(charSeq) : return true if the given string begins with charSeq else return false.
            eg:
                boolean b2 = s1.startWith("java");
                System.out.println(b2);
                output : true

        7. endsWith(charSeq) : return true if the given String end with charSeq else return false.
            eg:
                boolean b3 = s1.endsWith("udy");
                System.out.println(b3)
                output : true

Example Program1 :

class Program1
{
public static void main(String args[])
{
System.out.prinln("Program Started");
String s1 = "javastudy";
System.out.println("given string is ="+s1);
int x1 = s1.length();
System.out.println("total character"+x1);
char c1 = s1.charAt(4);
System.out.println("char @ position 4 is"+c1);
int x2 = s1.indexOf(t);
Syste.out.println("first occurance of t is"+x2);
int x3 = s1.indexOf('t',6);
Syste.out.println("first occurance of t is"+x3);
int x4 = s1.lastIndexOf('a');
System.out.println("last occurance of string "+x4)
System.out.prinln("Program Ended");
}
}



Example Program2 :

class Program2
{
public static void main(String args[])
{
System.out.prinln("Program Started");
String s1 = "javastudy";
System.out.println("given string is ="+s1);
boolean b1= s1.contains("dev");
System.out.println("contains dev ?"+b1);
boolean b2= s1.startsWith("java");
System.out.println("starts with java ?"+b2);
boolean b3= s1.endsWith("per");
Syste.out.println("ends with java ?"+b3);
String s2 = s1.substring(8);
Syste.out.println(s2);
String s3 = s1.substring(4,11);
Syste.out.println(s3);
System.out.prinln("Program Ended");
}
}


for more details follow vide given below: 




    

Comments

Popular Posts