Java Strings

Java Strings, What is String?, Declare Strings in Java, String handling in Java, String Methods, String Concatenation, and String Comparison.

Strings in Java are objects, and they are immutable. Whenever a change to a String is made, an entirely new String is created.

String Handling in Java

What is String?

The string is a sequence of characters written in double-quotes.

Syntax:

String stringName/VariableName = “Value”;

Example:

String val=”India”;

In Java, string represents sequence of char values, An array of characters works same as Java string.

Ex: char[] ch={‘G’,’C’,’R’,’e’,’d’,’d’,’y’};

String s=new String(ch);

is same as:

String s=”GCReddy”;

String may contain Alphabets, Numbers, and special characters

Example:

String a=”INDIA”;
String b=”india”;
String c=”INdia”;
String f=”%^&”;
String g=”India123%^*”;
String l=”Selenium”;
String m=”Automated Testing using Selenium”;

String d=”100″;
int r=100;

String e=”123.34″;
double x=123.34;

String h=”A”;
char q=’A’;

String n=”true”;
boolean p=true;
————–
100
10.34
true
A

Karun

Hyderabad

A123

AP28 1234

Operations on Strings:

1. Concatenating Strings

String + String – String
String + Number – String
Number + Number – Addition

Example:

String str1= “Selenium”;
String str2 = ” Testing”;
//String concatenation using + operator
System.out.println(str1+str2);//Selenium Testing

//String concatenation using “concat” Method
System.out.println(str1.concat(str2));//Selenium Testing

System.out.println(“Selenium”+”Testing”);//SeleniumTesting
System.out.println(“Selenium”+1+1);//Selenium11
System.out.println(1+1+”Selenium”);//2Selenmium
System.out.println(1+11);//12

2. String Comparison

In Computer programming we have two types of comparisons,
1. 2-way Comparison (Logical/ true or false)
2. 3-way Comparison (Zero, Greater than Zero, Less than Zero)

Ways of String Comparison

a. String Comparison using Relational Operator (==)
It supports a 2-way comparison

b. String Comparison using equals() method
It supports a 2-way comparison

c. String Comparison using compareTo() method
It supports 3-way Comparison

Result criteria for 3-way comparison

if string1 == string2 then 0
if string1 > string 2 then Positive Value
if string1 < string 2 then Negative value

Comparing two number – based on their values (3>2)
Comparing two strings – based on ANSI values
3>2 – false
“Delhi” > “Mumbai” – false
“delhi” > “Mumbai” – true

ANSI character codes

A to Z (65 to 90)
a to z (97 to 122)
0- to 9 (48 to 57)

Example:

String str1 = “SELENIUM”;
String str2 = “selenium”;
String str3 = “SELENIUM”;
String str4 = “zsele”;

//String Comparison using Relational (==) Operator
System.out.println(str1 == str2);//false
System.out.println(str1 == str3);//true

//String Comparison using equals() Method
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true

//String comparison using compareTo()
System.out.println(str1.compareTo(str2));//Negative value
System.out.println(str1.compareTo(str3));//0
System.out.println(str4.compareTo(str1));//Positive value


3. charAt() Method

Returns a Character value by Index

Example:

String str1= “Selenium”;

System.out.println(str1.charAt(1));//e
System.out.println(str1.charAt(7));//m

4. equalsIngoneCase();

It compares two strings and ignores letters (Upper case or Lower case)

Example:

String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “UFT”;

System.out.println(str1.equalsIgnoreCase(str2));//true
System.out.println(str2.equalsIgnoreCase(str3));//false

5. toUpperCase() Method

It converts values to Upper Case

Example:

String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “SELEnium”;
String str4 = “Selenium123”;

System.out.println(str1.toUpperCase()); //SELENIUM
System.out.println(str2.toUpperCase());//SELENIUM
System.out.println(str3.toUpperCase());//SELENIUM
System.out.println(str4.toUpperCase());//SELENIUM123

6. toLowerCase() Method

It converts values to lower case

Example:

String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “SELEnium”;
String str4 = “Selenium123”;

System.out.println(str1.toLowerCase()); //selenium
System.out.println(str2.toLowerCase());//selenium
System.out.println(str3.toLowerCase());//selenium
System.out.println(str4.toLowerCase());//selenium123

7. trim() Method

It removes spaces from both sides of a string

Example:

String str = ” Selenium “;
System.out.println(str);
System.out.println(str.trim());

8. substring() method

Returns part of a string based on index position/s

Example:

String str = “Welcome to Selenium Testing”;

System.out.println(str.substring(11));//Selenium Testing
System.out.println(str.substring(20));//Testing
System.out.println(str.substring(11, 19));//Selenium
System.out.println(str.substring(8, 10));//to

9. endsWith() Method

It checks whether a string ends with a specified suffix or not, And supports a 2-way comparison (true/false)

Example:

String str = “Welcome to Selenium Testing”;

System.out.println(str.endsWith(“Selenium Testing”));//true
System.out.println(str.endsWith(“Testing”));//true
System.out.println(str.endsWith(“Selenium”));//false

10. length 

It Returns the length of a String

Example:

String str = “Selenium Testing”;
String str2 = “Selenium”;
System.out.println(str.length());//16
System.out.println(str2.length());//8

 


Java Language Syllabus

Java Videos

Follow me on social media:

3 thoughts on “Java Strings”

Comments are closed.