INTRODUCTION
We all start our programming journey, irrespective of the language we choose, with the message "HELLO WORLD!". A String is nothing but a collection of characters. In this case, it is a collection of words forming "HELLO WORLD!". This means we all are familiar with String but, let us, deep dive, into this easy but most important topic in Java and understand various methods of String. So without any further ado
String : Definition
In Java, the string is basically an object that represents a sequence of char values. An array of characters works the same as a Java string. Strings are used for storing sentences/text. A String variable contains a collection of characters surrounded by double quotes "< text >"
Though String is a class it can be used as a data type which you can see from the above example. It is a non-primitive datatype. Remember that, a String is immutable and cannot be changed, we can sort of update it but the original value remains in the memory once assigned!
A String is instantiated as follows in Java :
DIFFERENT METHODS OF PRINTING
Before understanding various methods understand this :
We can use the following methods to print in Java Language :
In the above method, System.out.format() and System.out.printf() are the same so it depends on the Developer as to which to choose. Let's see how to use them with an example :
String Methods
They are methods in String to find out about various aspects of String. For example about its length, etc. To convert the existing string into lower/upper case. Remember as mentioned earlier the string is immutable and it does not change, the original string remains in the memory. Let me now tell you all the methods in string :
๐ฃ Some of the commonly used String methods are ๐ฃ
๐1) name.length() - Tells us the length of the string which is 7 in this case
๐2) name.toLowerCase() - Returns a new String that has all the characters in lowercase
๐3) name.toUpperCase() - Returns a new string that has all the characters in uppercase
๐4) name.trim() - Returns a new String after removing all the leading and trailing spaces from the original string
๐5) name.substring(int start) - Return a substring from start to end
๐6) name.substring(int start, int end) - Return a substring from int start way upto the int before the int end. The last index no. is always excluded
๐7) name.replace("char to be replaced", "char to be added") - Returns a new string after the the replacement
๐8) name.startswith("Vi") - Returns true if the string starts with "Vi"
๐9) name.endswith("ak") - Returns true if the string ends with "ak"
๐10) name.charAt (index number) - Returns the char present in the mentioned index position
๐11) name.indexOf("char") - Returns index of the given string. Here in the below example, "a" is in the 3rd position also and the 5th position also but it will return the 3rd value because it is first! Returns -1 if match not found
๐12) name.indexOf("char", index number) - Return the index of the given string starting from the index position mentioned after the comma, and everything before is ignored! Returns -1 if match not found
๐13) name.lastIndexOf("r") - Returns the last index of the given String
๐14) name.lastIndexOf("r",3) - Returns the last index of the given string before index 3
๐15) name.equals("Vinayak") - Returns true if equal otherwise false
๐16) name.equalsIgnoreCase("vinayak") - Returns true if equal without comparing case of the string
That's A wrap!
Thanks for reading my Article๐. Hope it adds value to the community๐
Please Like, Share and comments are appreciated๐๐ป
Follow me on Hashnode VINAYAK PANCHAL