Java String

String in Java

A string in Java is a sequence of characters used to represent text. It is one of the most used data types in programming and is essential for handling textual data. Here’s a comprehensive explanation of what a string is, its characteristics, and how it is used:

Creating a simple string with a value

String name = “Justin”;

Gets the number of characters in the string

int size = name.length(); //where size is a variable with int type and name.length the method to get the length of string name (Justin).
System.out.println(size); // Output will be 6

Joining 2 strings using concatenation format

String firstname = “Justin”;
String lastname = “bieber”;
System.out.println(firstname + ” ” + lastname); // Output will be Justin bieber

This line prints a concatenated string to the console. The + operator is used to concatenate the strings. Here, firstname + ” ” + lastname combines the strings with a space between Justin & bieber, resulting in “Justin bieber.”

Joining 3 strings using placeholder format

String firstname = “Justin”;
String middlename = “hudson”;
String lastname = “bieber”;

System.out.println(String.format(“%s %s”, firstname, lastname)); // Output will be Justin bieber
System.out.println(String.format(“%s %s %s”, firstname, middlename, lastname)); // Output will be Justin hudson bieber

%s is used as a placeholder for strings in String.format, with each subsequent argument replacing the respective placeholder.

Convert string to all uppercase characters

String name = “Justin”;
String capital = name.toUpperCase();
System.out.println(capital); // Output will be JUSTIN

Convert string to all lowercase characters

String name = “Justin”;
String lowercase = name.toLowerCase();
System.out.println(lowercase); // Output will be justin

Retrieves a substring from the string

String name = “Justin”;
String substring = name.substring(0, 4);
System.out.println(substring); // Output will be Just

The substring() method extracts a portion of the string. The first parameter (0) specifies the starting index, and the second parameter (4) specifies the length of the substring.

Character/Length

1

2

3

4

5

6

Index

0

1

2

3

4

5

Removes all starting and ending white-space characters

String name = ” Justin drake “;
String space = name.trim();
System.out.println(space); // Output will be “Justin drake”

The trim() method removes any leading and trailing spaces from the string.
It will remove the blank spaces marked in blue above

Determines whether a string contains a specified substring

String name = “Justin drake”;
boolean shortname = name.contains(“Justin”);
System.out.println(shortname); // Output will be true

Determines whether a string starts with a specified substring

String name = “Justin”;
boolean starting = name.startsWith(“Ju”);
System.out.println(starting); // Output will be true

Determines whether a string ends with a specified substring

String name = “Justin”;
boolean ending = name.endsWith(“!”);
System.out.println(ending); // Output will be false

Determines whether the value in the string is the same as the value mentioned in the equals() method

String name = “Justin”;
boolean value = name.equals(“Hello, World!”);
System.out.println(value); // Output will be false

Copies one string variable value to another string variable

String name = “Justin”;
String duplicate = new String(name);
System.out.println(duplicate); // Output will be Justin

Replaces words or characters in a string

String name = “Justin bieber”;
String changeword = name.replace(“Justin”, “Drake”);
String changecharacter = name.replace(‘b’, ‘B’);
System.out.println(changeword); // Output: Drake bieber
System.out.println(changecharacter); // Output: Justin BieBer

Insert a value into a string variable Java doesn’t have a direct Insert() method like C#. You can simulate this by combining substring and concatenation:
String name = “Justin”;
String newName = name.substring(0, 6) + ” bieber”;
System.out.println(newName); // Output: Justin bieber

The remove method Java doesn’t have a direct Remove() method like C#. You can achieve the same result using substring():
String name = “Justin drake”;
String shortened = name.substring(0, 5);
System.out.println(shortened); // Output will be Justi. It has taken first five characters in the name and removed rest of the characters.

Tasks:
1. Create a Simple String: Create a string variable called myName and assign it your name. Print the string to the console.
2. Get Length of String: Write a program to get the number of characters in the string myName and print the length.
3. Concatenation: Create two string variables firstName and lastName with your first and last names. Concatenate them using the + operator and print the result. Repeat the above step using string interpolation. Repeat the above step using placeholders.
4. Case Conversion: Convert the string myName to uppercase and print the result. Convert the string myName to lowercase and print the result.
5. Substring: Extract a substring from myName that includes the first three characters and print it. Extract the last three characters of myName and print them.
6. Trim: Create a string with leading and trailing white spaces and print it. Then, remove the white spaces using the trim method and print the result.
7. Contains: Check if the string myName contains a specified substring (e.g., your first name) and print the result.
StartsWith and EndsWith: Check if the string myName starts with your first name and print the result. Check if the string myName ends with your last name and print the result.
8. Equals: Create another string variable with the same value as myName and check if they are equal using the equals method. Print the result.
9. Copy: Copy the value of myName into another string variable called copiedName and print it.
10. Replace: Replace your first name in the string myName with another name and print the result. Replace all occurrences of a specific character in myName with another character and print the result.
11. Insert: Insert a middle name into the string myName at the appropriate position and print the result.
12. Remove: Remove the last name from the string myName and print the result.

Course Video

YouTube Reference :