Saturday, February 5, 2022

Strings

In Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.

String contains an immutable sequence of Unicode characters.


1. Create a String:

//Using String literal

String str1 = “Welcome 1”;

//Using new keyword

str2 = new String(“ Welcome1 ”);


2. Immutable String:

class Stringimmutable

{

public static void main(String args[]) {

String s=“SoftwareTesting”;

s.concat(“Strings”);

}

}


3. Methods of Strings:

//compares address

str1==str2 ;

//compares the values

String newStr = str1.equals(str2);

//compares the values ignoring the case

String newStr = str1.equalsIgnoreCase();

//calculates length

newStr = str1.length();

//extract i’thcharacter

newStr = str1.charAt(i);

//returns string in ALL CAPS

newStr = str1.toUpperCase();

//returns string in ALL LOWERvsCASE

newStr = str1.toLowerCase();

//search and replace

newStr = str1.replace(oldVal, newVal);

//trims surrounding whitespace

newStr = str1.trim();

//check for the values

newStr = str1.contains(“value”);

//convert String to character type array

newStr = str1.toCharArray();

//Check for empty String

newStr = str1.IsEmpty();

//Check if string ends with the given suffix

newStr = str1.endsWith();


STRING CONVERSIONS:

1. String to Int Conversion:

//Converting a string to int

String str=“123”;

int inum1 = 10;

int inum2 = Integer.parseInt(str);

2. Int to String Conversion:

// Conversion of Int to String

int var = 211;

String str = String.valueOf(var);

System.out.print1n(111+str);

3. String to Double Conversion:

//displaying the value of variable dnum

String str = “111.222”;

double dnum = Double.parseDouble(str);

4. Double to String Conversion:

//conversion using valueOf() method

//double value

double dnum = 11.222;

String str = String.valueOf(dnum);


This article focuses on Strings Basics, its methods and Conversions.

Thanks for reading my article..!!!!

Happy Strings!!!!