Java String Class MethodsThe java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java. By the help of these methods, we can perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc. Java String is a powerful concept because everything is treated as a String if you submit any form in window based, web based or mobile application. Let's use some important methods of String class. Java String toUpperCase() and toLowerCase() methodThe Java String toUpperCase() method converts this String into uppercase letter and String toLowerCase() method into lowercase letter. Stringoperation1.java Test it NowOutput: SACHIN sachin Sachin Java String trim() methodThe String class trim() method eliminates white spaces before and after the String. Stringoperation2.java Test it NowOutput: Sachin Sachin Java String startsWith() and endsWith() methodThe method startsWith() checks whether the String starts with the letters passed as arguments and endsWith() method checks whether the String ends with the letters passed as arguments. Stringoperation3.java Test it NowOutput: true true Java String charAt() MethodThe String class charAt() method returns a character at specified index. Stringoperation4.java Test it NowOutput: S h Java String length() MethodThe String class length() method returns length of the specified String. Stringoperation5.java Test it NowOutput: 6 Java String intern() MethodA pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a String equal to this String object as determined by the equals(Object) method, then the String from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. Stringoperation6.java Test it NowOutput: Sachin Java String valueOf() MethodThe String class valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into String. Stringoperation7.java Output: 1010 Java String replace() MethodThe String class replace() method replaces all occurrence of first sequence of character with second sequence of character. Stringoperation8.java Output: Kava is a programming language. Kava is a platform. Kava is an Island. Next TopicStringBuffer class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India