Pages

Friday, 20 May 2022

STRINGS

In Java, a string is a sequence of characters. String is basically an object that represents sequence of char values. An array of characters works same as Java string. We use double quotes to represent a string in Java.

Ex:
"hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l', 'o'.
// create a string
String type = "Java programming";

Here, we have created a string variable named type. The variable is initialized with the string Java Programming.

Note: Strings in Java are not primitive types (like int, char, etc). Instead, all strings are objects of a predefined class named String. And, all string variables are instances of the String class.

String Class
String is one of the standard classes of java. An Object of string class represent collection of characters(string). It provides methods to manipulate strings.

Note: String represented by String class is immutable. That means once we create a sting, its value cannot be modified.

Ex: Create a String in Java 
class Main
{
public static void main(String[] args)
{
// create strings 
String first = "Java";
String second = "Python"; 
String third = "Oracle";
// print strings
System.out.println(first); // prints Java 
System.out.println(second); // prints Python 
System.out.println(third); // prints Oracle
}
}
Output:
Java
Python
Oracle

Java String Operations
public class StrEx
{
public static void main(String args[])
{
//creating a string by java string literal 
String str = "Oraclebook";
char arrch[]={'h','e','l','l','o'};
//converting char array arrch[] to string str2 
String str2 = new String(arrch);
//creating another java string str3 by using new keyword 
String str3 = new String("Java String Example");
//Displaying all the three strings 
System.out.println(str); 
System.out.println(str2); 
System.out.println(str3);
}
}
Output:
Oraclebook 
hello
Java String Example

charAt() : This method returns the character at the specified index. syntax:
string.charAt(int index)

Note: string is an object of the String class. 
class Main1
{
public static void main(String[] args)
{
String str1 = "Learn Java"; 
String str2 = "Learn\nJava";
// first character 
System.out.println(str1.charAt(0)); // 'L'
// seventh character 
System.out.println(str1.charAt(6)); // 'J'
// sixth character 
System.out.println(str2.charAt(5)); // '\n'
}
}
output:
L
J
 
length() : This method is Used to count number of characters in a given string including blank spaces 
Ex:
class Main1
{
public static void main(String[] args)
{
// create a string
String greet = "Hello! World"; 
System.out.println("String: "+greet);
// get the length of greet 
int length = greet.length();
System.out.println("Length(No of characters including white spaces): "+length);
}
}
Output:
String: Hello! World
Length(No of characters including white spaces): 12

concat() : This method is Used to join two Strings 
class Main1
{
public static void main(String[] args)
{
// create first string 
String first = "Java ";
System.out.println("First String: " + first);
// create second
String second = "Programming"; 
System.out.println("Second String: " + second);
// join two strings
String concatString = first.concat(second); 
System.out.println("Concatinated String: " + concatString);
}
}
Output:
First String: Java
Second String: Programming 
Joined String: Java Programming

equals(): This method is used to Compare two Strings among the given strings - we use equals() method. 
class Main1
{
public static void main(String[] args)
{
// create 3 strings
String first = "java programming"; 
String second = "java programming"; 
String third = "python programming";
// compare first and second strings 
boolean result1 = first.equals(second);
System.out.println("First Strings and Second Strings Are Equal: " + result1);
// compare first and third strings 
boolean result2 = first.equals(third);
System.out.println("First Strings and Third Strings Are Not Equal: " + result2);
}
}
Output:
First Strings and Second Strings Are Equal: true 
First Strings and Third Strings Are Not Equal: false

substring() : It extracts a substring from the string and returns it. Syntax:
string.substring(int startIndex, int endIndex) 
Note : string is an object of the String class.

Ex: Java substring() Without End Index 
class Main1
{
public static void main(String[] args)
{
String str1 = "program";
// from the first character to end 
System.out.println(str1.substring(0)); // program
// from the 4th character to end 
System.out.println(str1.substring(3)); // gram
}
}
Output:
program
gram

Ex 2: Java substring() With End Index 
class Main1
{
public static void main(String[] args)
{
String str1 = "program";
// from 1st to the 7th character
System.out.println(str1.substring(0, 7)); // program
// from 1st to the 5th character 
System.out.println(str1.substring(0, 5)); // progr
// from 4th to the 5th character 
System.out.println(str1.substring(3, 5)); // gr
}
}
Output:
program
progr
gr


indexOf() : The String indexOf() method returns the index of the first occurrence of the specified character/substring within the string.
Syntax:
string.indexOf(int ch, int fromIndex) or
string.indexOf(String str, int fromIndex)
Note1: string is an object of the String class.
Note2: indexOf() Return Value
returns the index of the first occurrence of the specified character/string returns -1 if the specified character/string is not found.

Ex1: Java String indexOf() with only one parameter 
class Main
{
public static void main(String[] args)
{
String str1 = "Learn Java"; 
int result;
// getting index of character 'J' 
result = str1.indexOf('J'); 
System.out.println(result); // 6
// the first occurrence of 'a' is returned 
result = str1.indexOf('a'); 
System.out.println(result); // 2
// character not in the string 
result = str1.indexOf('j'); 
System.out.println(result); // -1
// getting the index of "ava" 
result = str1.indexOf("ava"); 
System.out.println(result); // 7
// substring not in the string 
result = str1.indexOf("java"); 
System.out.println(result); // -1

// index of empty string in the string 
result = str1.indexOf("");
System.out.println(result); // 0
}
}
Output:
6
2
-1
7
-1
0

Ex2: indexOf() With fromIndex Parameter 
class Main
{
public static void main(String[] args)
{
String str1 = "Learn Java programming"; 
int result;
// getting the index of character 'a'
// search starts at index 4 
result = str1.indexOf('a', 4); 
System.out.println(result); // 7
// getting the index of "Java"
// search starts at index 8
result = str1.indexOf("Java", 8); 
System.out.println(result); // -1
}
}
Output:
7
-1

toLowerCase() : The Java String toLowerCase() method converts all characters in the string to lower case characters.

Syntax: 
string.toLowerCase()

Note: string is an object of the String class.

Ex:
class Main
{
public static void main(String[] args)
{
String str1 = "Learn Java"; 
String str2 = "Java123";
// convert to lower case letters 
System.out.println(str1.toLowerCase()); // "learn java" 
System.out.println(str2.toLowerCase()); // "java123"
}
}
Output:
learn java 
java123
 
toUpperCase(): The Java String toUpperCase() method converts all characters in the string to upper case characters.

Syntax: 
string.toUpperCase()

Ex:
class Main
{
public static void main(String[] args)
{
String str1 = "Learn Java"; 
String str2 = "Java123";
// convert to upper case letters 
System.out.println(str1.toUpperCase()); // "LEARN JAVA" 
System.out.println(str2.toUpperCase()); // "JAVA123"
}
}
Output:
LEARN JAVA 
JAVA123

replace() : The Java String replace() method replaces each matching occurrences of the old character/text in the string with the new character/text.

Syntax:
string.replace(char oldChar, char newChar) or
string.replace(CharSequence oldText, CharSequence newText) 
Note: string is an object of the String class.

Ex: Java String replace() Characters 
class Main
{
public static void main(String[] args)
{
String str1 = "abc cba";
// all occurrences of 'a' is replaced with 'z' 
System.out.println(str1.replace('a', 'z')); // zbc cbz
// all occurences of 'L' is replaced with 'J' 
System.out.println("Lava".replace('L', 'J')); // Java
// character not in the string 
System.out.println("Hello".replace('4', 'J')); // Hello
}
}
Output:
zbc cbz
Java
Hello

Ex2: Java String replace() Substrings 
class Main
{
public static void main(String[] args)
{
String str1 = "C++ Programming";
// all occurrences of "C++" is replaced with "Java" 
System.out.println(str1.replace("C++", "Java")); // Java Programming
// all occurences of "aa" is replaced with "zz" 
System.out.println("aa bb aa zz".replace("aa", "zz")); // zz bb aa zz
// substring not in the string 
System.out.println("Java".replace("C++", "C")); // Java
}
}
Output:
Java Programming
zz bb zz zz
Java

split() : It divides the string into multiple strings based on the given delimiter. 
syntax:
string.split(String regex, int limit)

Note1: string is an object of the String class. 
Note2:
split() Parameters : The string split() method can take two parameters: They are delimiter & limit. 
delimiter / regex - the string is divided at this delimiter / regex (can be strings)
limit (optional) - controls the number of resulting substrings
If the limit parameter is not passed, split() returns all possible substrings.

Ex: split() Without limit Parameter
// importing Arrays to convert array to string
// used for printing arrays  
import java.util.Arrays;
class Main
{
public static void main(String[] args)
{
String vowels = "a::b::c::d::e";
// splitting the string at "::"
// storing the result in an array of strings 
String[] result = vowels.split("::");
// converting array to string and printing it 
System.out.println("result = " + Arrays.toString(result));
}
}
Output
result = [a, b, c, d, e]

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...