Monday, July 2, 2018

12 to 2 batch : 02 - JULY - 2018

public class StringDemo01
{
public static void main(String[] args)
{
String str = "length";

// get the length of string and store inside variable 'l'
int l = str.length();

System.out.println("length is "+l);
}
}

# # # # # # # # # # # #

public class StringDemo02
{
public static void main(String[] args)
{
String str = "  This    That       ";

System.out.println(">"+str+"<");

// trim the string and store it inside 'str'
str = str.trim();

System.out.println(">"+str+"<");

}
}

# # # # # # # # # # # #

public class StringDemo03
{
public static void main(String[] args)
{
String str = "Lion";

// get a char at a particular index
// here we are getting the char at index 0
char ch = str.charAt();

System.out.println(ch);
}
}

# # # # # # # # # # # #

public class StringDemo04
{
public static void main(String[] args)
{
String str1 = "BIG 1 && small 1";

// convert str1 to upper case and store inside
//str2
String str2 = str1.toUpperCase();

// convert str1 to lower case and store inside
//str3
String str3 = str1.toLowerCase();

System.out.println("str1: "+str1);
System.out.println("str2: "+str2);
System.out.println("str3: "+str3);
}
}


# # # # # # # # # # # #

public class StringDemo05
{
public static void main(String[] args)
{
String str = "dis  is a cat dis is a bat";

// find 'dis' and replace with 'this'
str = str.replaceAll("dis","this");

// find 'space' and replace with '_'
str = str.replaceAll(" ","_");

System.out.println(str);
}
}


# # # # # # # # # # # #

public class StringDemo06
{
public static void main(String[] args)
{
String str = "012 abc ABC=";

// convert string into array of byte and store inside array 'ba'
byte[] ba = str.getBytes();

// show array of byte
System.out.println("# array of byte #");
for (int i = 0; i < ba.length; i++)
{
System.out.println(i+" : "+ba[i]);
}

// convert string into array of char and store inside array 'ca'
char[] ca = str.toCharArray();

// show array of char
System.out.println("# array of char #");
for (int i = 0; i < ca.length; i++)
{
System.out.println(i+" : "+ca[i]);
}

}
}

# # # # # # # # # # # #

public class StringDemo07
{
public static void main(String[] args)
{
String str1 = "i am yello";
String str2 = "i am yello";

// compare content of str1 and str2 (case sensitive)
if(str1.equals(str2))
{
System.out.println("strings has same data");
}
else
{
System.out.println("strings doed not has same data");
}

// compare content of str1 and str2 (case sensitive)
if(str1.equalsIgnoreCase(str2))
{
System.out.println("strings has same data");
}
else
{
System.out.println("strings doed not has same data");
}

}
}

# # # # # # # # # # # #

public class StringDemo08
{
public static void main(String[] args)
{
String str = "Let us C";

// get a substring from index 4 till last
String str1 = str.substring(4);
System.out.println(str1);

// get a substring from index 0 to index 6 minus 1
String str2 = str.substring(0,6);
System.out.println(str2+" with milk");

}
}

# # # # # # # # # # # #

import java.util.Scanner;

public class StringAssignment01
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.print("Input a text: ");
String str = sc.nextLine();

// get length of string
int len = str.length();

// check if length of string is between 5 and 8
if(len >= 5 && len <= 8)
{
System.out.println("valid length of string");
}
else
{
System.out.println("invalid length of string");
}
}
}

# # # # # # # # # # # #

public class StringTestOne
{
public static void main(String[] args)
{
String str = "Jai hanuman gyan gun sagar !!";

for(int i = 0; i < str.length(); i++)
{
System.out.print(i+":");
String str2 = str.substring(i);
System.out.println(str2);
}
}
}

# # # # # # # # # # # #

public class StringTestTwoWithPrize
{
public static void main(String[] args)
{
String str = "Jai hanuman gyan gun sagar !!";

for(int i = 0; i < str.length(); i++)
{
System.out.print(i+":");
str = str.substring(i);
System.out.println(str);
}
}
}

No comments:

Post a Comment