Java: What is a String and Its Methods๐Ÿงต

Java: What is a String and Its Methods๐Ÿงต

- VINAYAK PANCHAL

ยท

4 min read

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 LetsStartItMagnusNielsenGIF.gif

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 >"

Untitled.png

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 :

Reference.png

DIFFERENT METHODS OF PRINTING

Before understanding various methods understand this :

Reference (1).png

We can use the following methods to print in Java Language :

Reference (2).png

System.out.printf(%d, int).png

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 :

Reference (4).png

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 :

Reference (5).png

๐ŸŸฃ Some of the commonly used String methods are ๐ŸŸฃ

๐Ÿ“Œ1) name.length() - Tells us the length of the string which is 7 in this case

Reference (6).png

๐Ÿ“Œ2) name.toLowerCase() - Returns a new String that has all the characters in lowercase

Reference (7).png

๐Ÿ“Œ3) name.toUpperCase() - Returns a new string that has all the characters in uppercase

Reference (8).png

๐Ÿ“Œ4) name.trim() - Returns a new String after removing all the leading and trailing spaces from the original string

Reference (9).png

๐Ÿ“Œ5) name.substring(int start) - Return a substring from start to end

System.out.printf(%d, int) (1).png

๐Ÿ“Œ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

System.out.printf(%d, int) (2).png

๐Ÿ“Œ7) name.replace("char to be replaced", "char to be added") - Returns a new string after the the replacement

System.out.printf(%d, int) (3).png

๐Ÿ“Œ8) name.startswith("Vi") - Returns true if the string starts with "Vi"

System.out.printf(%d, int) (4).png

๐Ÿ“Œ9) name.endswith("ak") - Returns true if the string ends with "ak"

System.out.printf(%d, int) (5).png

๐Ÿ“Œ10) name.charAt (index number) - Returns the char present in the mentioned index position

System.out.printf(%d, int) (6).png

๐Ÿ“Œ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

System.out.printf(%d, int) (7).png

๐Ÿ“Œ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

System.out.printf(%d, int) (8).png

๐Ÿ“Œ13) name.lastIndexOf("r") - Returns the last index of the given String

System.out.printf(%d, int) (9).png

๐Ÿ“Œ14) name.lastIndexOf("r",3) - Returns the last index of the given string before index 3

System.out.printf(%d, int) (10).png

๐Ÿ“Œ15) name.equals("Vinayak") - Returns true if equal otherwise false

System.out.printf(%d, int) (11).png

๐Ÿ“Œ16) name.equalsIgnoreCase("vinayak") - Returns true if equal without comparing case of the string

System.out.printf(%d, int) (12).png

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

ย