1.What is mean by String in Java?
Strings are used for storing text.
A String variable contains a collection of characters surrounded by double quotes (""):
(https://www.w3schools.com/java/java_strings.asp)
A String in Java is an object used to store a sequence of characters enclosed in double quotes. It uses UTF-16 encoding and provides methods for handling text data.
Each character in a string is stored using 16-bit Unicode (UTF-16) encoding.
Strings are immutable, meaning their value cannot be changed after creation.
(https://www.geeksforgeeks.org/java/strings-in-java/)
2.String Methods
String length()
It will Show the total characters in a one String Variable
the main work of the String length() method is to return the total length Characters present inside a String
the return datatype of the length method is int
Here by using the length() method we can calculate the total Characters of the variable food which is stored inside a datatype string
the output of this code is 8
to convert full String to a capital letter or full String to a small letter we have a method called
toUpperCase()
toLowerCase()
when we use toUpperCase() Method the String will be automatically converted into Capital Letters
when we use toLowerCase() Method the String will be automatically converted into Small Letters
the output of the code will be
BIRIYANI //toUpperCase()
biriyani //toLowerCase()
Finding a Character in a String
The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace):
the return datatype of this method is int
Java counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third ...
the output of the code will be
5
The return datatype of this method is char
Now in the String food = "Biriyani"; i want to take the word riya from this
Now here comes the super method
substring()
syntax of this method
substring(beginIndex , endIndex)
beginIndex -> included
endIndex -> excluded
The substring() method is used to extract (or retrieve) a portion of a string.
It returns a new String containing the selected characters from the original string.
The return datatype of this method is String
the output of the code will be Biri
Now guess the output of this
There are two overloaded substring() methods.
one is substring(int beginIndex, int endIndex)
Starts from beginIndex and stops before endIndex.
the another one is substring(int beginIndex)
Starts from the given index and returns characters up to the end of the string.
So the output of the code is iyani
Comparing Strings
To compare two strings, you can use the equals() method:
the return datatype of this method is a boolean it tells true or false
The output of the code will be true
Removing Whitespace
The trim() method removes whitespace from the beginning and the end of a string:
the return datatype of this method is String
the output will be
Before: [ Hello World ]
After: [Hello World]
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation
Output
Hariharan Sivasankar
The concat() Method
You can also use the concat() method to concatenate strings:
the return datatype of the concat method is String
String firstName = "Hari ";
String lastName = "Bavan";
System.out.println(firstName.concat(lastName));
output
Hari Bavan
Adding Numbers and Strings
In Java uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
int x = 10;
int y = 20;
int z = x + y;
System.out.println(z);
output
30
If you add two strings, the result will be a string concatenation:
String x = "45";
String y = "18";
String z = x + y;
System.out.println(z);
output
4518// (a string)
If you add a number and a string, the result will be a string concatenation:
String x = "7";
int y = 3;
String z = x + y;
System.out.println(z);
output
73// (a string)












Top comments (0)