String Method in Java
String Method in Java. In this Article we will step into some of the method ,which we can be used to manipulate String Data in Java.
In Java a function is known as method. Methods are predefined function use to conduct a specific task of execution use to manipulate data.There are native method which are being created just from the beginning of time, and function that we can create to execute specific tasks.
There are a lot java method Library being developed , as usual you do not need to know or remember everything , you will go crazy and berserk if you try to put everything in your head. The Important thing is you know that they are there , and where can you find it . Refer this Link here for all the Strings Method
Refer the Table below for the widely Use String Methods
If you notice, all methods first word start with a lowercase, and the preceding word subsequently start with an Upper case. This is what they call a Camel case. Please be careful on that . Typo will cause you a syntax error.
No | Methods | Description |
---|---|---|
1 | length() | This will read out the Length of the String |
2 | charAt() | This will return a character value located at the given index number |
3 | concat() | This will combine specific string at the end of the string |
4 | contains() | This will return true when the character i the () are found in the string |
5 | startsWith() | This will check if the string is start with a given prefix |
6 | endsWith() | This will check if the string is end with a given prefix |
7 | equals() | This will compare the content of 2 given strings |
8 | indexOf | Returns of the given character value in a string |
9 | isEmpty() | Check if this string is empty |
10 | replace() | This will return a string replacing all old character into new character |
11 | substring() | This will return part of the string |
12 | toCharArray() | This will convert the string into character array |
13 | toLowerCase() | This will return the string into lower case |
14 | toUpperCase() | This will return the string into upper case |
15 | trim() | This will eliminate the leading and trailing space |
Check Out the Code Below
Calculate String Length
- first i created a Method which Calculate String Length
- Declare a global Variable for the Function to access
- As you know the return value is an integer , tus your global integer data type should be set to integer
- Call the Method which i have just created in Main and input my String value into ()
package firstproject; public class StringDemo { static int stringLength; public static void main(String[] args) { System.out.println( calculateStringLength("I am a String" )); } public static int calculateStringLength(String strExample ){ stringLength = strExample.length(); return stringLength; } }
Check whether the String is Empty
Let Me show one more example, While you guys can do the rest.The below result you will get one true and one false.
package firstproject; public class StringDemo { public static void main(String[] args) { String exampleString = ""; String exampleString2 = "I am a string"; System.out.println(exampleString.isEmpty()); System.out.println(exampleString2.isEmpty()); } }
Well this wrap up my article on String Method in Java.
Check out what is Global and local variable in Java here
Leave a Reply