I had a lot of trouble remember all the various steps in setting up and transferring an SSL certificates from an old domain to a new domain, so now that I recently did it, I wanted to document for…
A String is a class in Java and it can be seen as a collection or the sequence of characters. Strings are used as an object in Java. Java supports different methods for String Manipulation. In the next section, we will cover all the important string methods along with a brief description of each of them.
Java String class is an immutable class i.e. once it is created, it can’t be modified thereafter. This is the reason why StringBuffer and StringBuilder came into the picture as they are mutable and are used to do a lot of modification to the sequence of characters even after creation.
This blog has been mainly written for different Java String Methods, some of which are being mentioned below:
This method is used to retrieve a single character from a given String.
The syntax is given as:
Explain
The value of ‘i’ should not be negative and it should specify the location of a given String i.e. if a String length is 5, then the value of ‘i’ should be less than 5.
Given below is the program that will demonstrate how the charAt() method retrieves a particular character from the given String.
2. Length
The length is the number of characters that a given string contains. Java has a length() method that gives the number of characters in a String.
Example:
3. Concatenation
Although Java uses a ‘+’ operator for concatenating two or more strings. A concat() is an inbuilt method for String concatenation in Java.
Example:
4. String contains()
This method is used to determine whether a substring is a part of the main String or not. The return type is Boolean.
Example:
In the below program, we will check whether “testing” is a part of “Coding” or not and we will also check whether “blog” is a part of “Coding”.
5. Java String split()
As the name suggests, a split() method is used to split or separate the given String into multiple substrings separated by the delimiters (“”, “ ”, \\, etc). In the below example, we will split the String (Thexyzwebsitexyzisxyzsoftwaretestingxyzhelp) using a chunk of String(xyz) already present in the main String.
Example:
6. Java String indexOf()
This method is used to perform a search operation for a specific character or a substring on the main String. There is one more method known as lastIndexOf() which is also commonly used.
indexOf() is used to search for the first occurrence of the character.
lastIndexOf() is used to search for the last occurrence of the character.
Example:
7. String reverse()
The StringBuffer reverse() method is used to reverse the input characters of the String.
Example:
8. String replace()
The replace() method is used to replace the character with the new characters in a String.
Example:
9. Java String compareTo()
This method is used to compare two Strings. The comparison is based on alphabetical order. In general terms, a String is less than the other if it comes before the other in the dictionary.
Example:
10. String to CharArray()
This method is used to convert all the characters of a string into a Character Array. This is widely used in the String manipulation programs.
Example:
I hope this blog comes handy to newbies trying and learning new languages with enthusiasm.
A follow means a lot :)
We all know there is a huge industry of dieting that coexist in a weird but efficient symbiosis with the fast-food industry. Every day we walk in a world that tries to sell you stuff at all costs…
And that is how I felt, and then I got addicted to the channel. What it does is hook you with the YouTube video title, and guess what the actual movie you are watching. Its most-watched video has…
Yikes. Apologies to those followed my advice last week (along with many others that I listened to). I could only muster up a total of 112 points, well short of the 142 needed to cash last week. Let’s…