Wednesday, June 27, 2018

Programs implemented in today class (string wali)

======================================

public class TheStringClass02 
{
public static void main(String[] args)
{
String str1 = "nat";
String str2 = "khat";

// using method
String str3 = str1.concat(str2);
System.out.println(str3);

// using + operator
String str4 = str1 + str2;
System.out.println(str4);
}
}

======================================

public class TheStringClass03
{
public static void main(String[] args)
{
int x = 123;

// convert any type of data into string
String str = String.valueOf(x == x);

System.out.println(str);
}
}

======================================

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

                // check if string str starts with 'cet' or not
System.out.println(str.startsWith("cet"));

                // check if string str ends with 'pa' or not
System.out.println(str.endsWith("pa"));
}
}


======================================

public class TheStringClass05 
{
public static void main(String[] args)
{
String str1 = "aam";
String str2 = "aaM meetha hai";

// compare str1 with str2 based on ASCII
// values of their length
int value = str1.compareTo(str2);

if(value == 0)
{
System.out.println("str1 == str2");
}
else if(value < 0)
{
System.out.println("str1 < str2");
}
else
{
System.out.println("str1 > str2");
}

}
}

======================================

public class TheStringClass06 
{
public static void main(String[] args) 
{
String str = "Cab10gjaj0";
// get the hashcode of string
int hc = str.hashCode();
System.out.println("hashcode of "+str+" is "
+hc); 
}
}

======================================

public class TheStringClass07 
{
public static void main(String[] args) 
{
String str = "internet";
// first index of n 
// search starts from index 0
int i = str.indexOf("n");
System.out.println("first index of n is "+i);

// first index of n 
// search start from index 2
int j = str.indexOf("n",2);
System.out.println("first index of n from index"
+ " 2 is "+j);
// last index of n
// search from index 0
int k = str.lastIndexOf("n");
System.out.println("last index of n is "+k);

// last index of n
// search from index 4
int l = str.lastIndexOf("n",4);
System.out.println("last index of n from "
+ " index 4 is "+l);
}
}

======================================

public class StringToPrimitive 
{
public static void main(String[] args) 
{
String str = "123";
// convert string into int
int a = Integer.parseInt(str);
System.out.println("a: "+a);

// convert string into float
float b = Float.parseFloat(str);
System.out.println("b: "+b);

// convert string into long
long c = Long.parseLong(str);
System.out.println("c: "+c);

// convert string into double
double d = Double.parseDouble(str);
System.out.println("d: "+d);

}
}

======================================
public class SubstringAssignmentOne 
{
public static void main(String[] args) 
{
String song = "chak de india !!";
for(int i = 0; i < song.length(); i++)
{
System.out.print(i+":");
String otherSong = song.substring(i);
System.out.println(otherSong);
}
}
}

======================================

public class SubstringAssignmentTwo
{
public static void main(String[] args) 
{
String song = "Jiyo re bahubali, oo";
for(int i = 0; i < song.length(); i++)
{
System.out.print(i+":");
song = song.substring(i);
System.out.println(song);
}
}
}

======================================

No comments:

Post a Comment