Useful String Methods - Part 1

Start course
Overview
Difficulty
Beginner
Duration
1h 31m
Students
28
Ratings
5/5
starstarstarstarstar
Description

In this course, we'll learn Strings in Java. 

Learning Objectives

  • What is String?
  • Creating String Objectives
  • Useful Methods of String Class
  • Immutable String
  • StringBuffer Class
  • StringBuilder Class

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we will learn some important methods of the string class. This string class provides many useful methods to perform string operations. As you see in the table, charAt method returns the char value for the particular index. It takes index parameter with int type. Length method returns the length of a string; it has no parameter. The substring method comes with two signatures. If we pass the only beginIndex, it returns a substring from the beginIndex to the end of the string. If we pass the beginIndex and endIndex to the method, then it returns a substring that starts from the begin index up to the end index minus one. The equals method compares the string with the specified string and returns true if two string objects contain the same contents, else returns false. isEmpty method returns true if the given string has 0 lengths. If the length of the string is non-zero, then returns false. The concat method concatenates the specified string str at the end of the string. toLowerCase method returns a string in lowercase.

toUpperCase method returns a string in uppercase. There are many more methods in the string class, but these are the more commonly used and known methods. Now, let's make some examples with these commonly used methods.

In string project, right click on the 'string' package and select 'New Class'. I will specify the class name as StringMethods, and select the checkbox for the main method. In this exercise, we will practice with string methods to understand clearly. Let's start to declare two string variables and a string array. First, we declare variable s1 and assign it a value "Java Programming Language". Secondly, I will declare variable s2  and assign it a value "Hello". And lastly, I will declare a string array that is named s3 that has no initial value. Now, let's use the methods of the string class. In the first print method, I will print the value of string s1. sout ("String: " + s1). Then, we use the charAt method to find the first character of string s1. sout ("The first character: " + s1.charAt(0)). The zeroth index gives us the first character of the string. So, I write 0 in parentheses. Then, I will use the length method to find the length of string s1. sout ("Length of String: " + s1.length()). Then, I'll use the substring method to print programming language to the console. sout ("Substring: " + s1.substring()). Notice that the index number of p, which is the first letter of programming language, is 5. In other words, if we give the value 5 to the substring method, it will print the programming language expression on the console. So, I write 5 in parentheses.

Then, I will use the equals method to compare s1 and s2 objects. sout ("isEqual ? " + s1.equals(s2)). Then, I will use the isEmpty method for checking if string s1 is empty or not. sout ("isEmpty ? " + s1.isEmpty()). Then, I'll use the concat method to concatenate s1 and s2 strings. sout ("concat: " + s2.concat(" " + s1)). Then, I will use the toUppercase method to get string s1 in uppercase letters. sout ("toUppercase: " + s1.toUpperCase()). Then, I will use the toLowercase method to get string s1 in lowercase letters. sout ("toLowercase: " + s1.toLowerCase()). Then, I'll use the split method to split the string s1 based on space. In split operation, we assign the result of the split method to string array as 3. So, I write s3 = s1.split(" "). Since there are two spaces in the s1 string, this will return a string array with three elements. The first element will be Java, the second element will be Programming, and the last element will be Language. Now, let's print this array on the console. sout ("Split base spaces: " + Array.toString(s3)). Finally, I'll use the replace method to replace part of the string with some other string. In the replace example, I want to replace the letter v in lowercase with the letter V in uppercase. So, I write sout ("replace v with V: " + s1.replace('v', 'V')). Okay, let's run the code. You see the results in the console.

The value of the string is Java Programming Language. The length of the string is 25, meaning the total number of characters of string s1. It also accepts spaces as a character. Do not forget this. Substring is programming language because we wrote 5 in parentheses and it was cut the first five characters of string by using the substring method. isEqual is false because s1 and s2 objects have different contents. isEmpty is false because string s1 has content. Concat is Hello Java Programming Language. We concat string s2 and s1 objects. toUppercase is JAVA PROGRAMMING LANGUAGE in uppercase letters. toLowercase is java programming language in lowercase letters. After the split method, an array with three elements is displayed on the console because the split method returns an array. The elements of the array are Java, Programming, and Language words because we split the s1 string according to the spaces. And last, after the replace method, we changed the letter V in uppercase in the first word JaVa. Yes, in this lecture, we did some examples to understand string methods. In the next video, we'll continue with a string methods. See you in the next video.

 

About the Author
Students
458
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics