Tuesday, July 3, 2018

12 to 2 batch : 03 - JULY - 2018

public class StringDemo09
{
public static void main(String[] args)
{
String str1 = "Mohe rang";
String str2 = " de basanti";

// combine using + operator
String str3 = str1 + str2;
System.out.println(str3);

// combine using concat method
String str4 = str1.concat(str2);
System.out.println(str4);
}
}

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

public class StringDemo10
{
public static void main(String[] args)
{
int a = 100;

// convert any type of data to string
String str = String.valueOf(a);
System.out.println(str);
}
}

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

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

// check if string starts with tamatar
if(str.startsWith("tamatar"))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
}
}

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

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

// get first index of 'N'
int i1 = str.indexOf("N");
System.out.println("first index from 0 is "+i1);

// get last index of 'N'
int i2 = str.lastIndexOf("N");
System.out.println("last index from 0 is "+i2);

// get first index of 'N' after index - 2
int i3 = str.indexOf("N",2);
System.out.println("first index from 2 is "+i3);

// get last index of 'N' before index - 2
int i4 = str.lastIndexOf("N",2);
System.out.println("last index from 2 is "+i4);
}
}

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

public class StringDemo13
{
public static void main(String[] args)
{
String str1 = "mangaL mangal";
String str2 = "mangal";

int res = str1.compareToIgnoreCase(str2);

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

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

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

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

// check string is empty
if(str.isEmpty())
{
System.out.println("string is empty");
}
else
{
System.out.println("string is not empty");
}
}
}

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

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

// get the hash value of string
int h = str.hashCode(); //

System.out.println("hashcode is "+h);
}
}

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

public class StringDemo16
{
public static void main(String[] args)
{
String str = "a\\b\\c\\d";

// split the string from the given delimiter
String[] array = str.split("\\\\");

for (int i = 0; i < array.length; i++)
{
System.out.println(i+":"+array[i]);
}
}
}

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

public class StringToPrimtive
{
public static void main(String[] args)
{
String str1 = "+12345";

// convert string into int
int a = Integer.parseInt(str1);
System.out.println("a: "+a);

String str2 = "100";

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

// convert string to boolean
boolean c = Boolean.parseBoolean("");
System.out.println("c: "+c);
}
}

No comments:

Post a Comment