Document Details

HearteningLiberty5038

Uploaded by HearteningLiberty5038

College of Engineering and Information Technology

Tags

Java programming string manipulation Java String methods programming

Summary

This document is a presentation or lecture notes on Java string methods. It explains how to use various methods in Java for string manipulation such as length(), toLowerCase(), and concatenation.

Full Transcript

Java String Java String String is a sequence of character treated as single unit such as “Hello World”. May include letters, digits, etc. String are immutable, once a string is created it cannot be changed. None of its methods changes the string. Creating a String T...

Java String Java String String is a sequence of character treated as single unit such as “Hello World”. May include letters, digits, etc. String are immutable, once a string is created it cannot be changed. None of its methods changes the string. Creating a String The common way to create a string is by using string literals enclosed in double quotes and assgning them to a variable. Syntax: String variableName = “String”; String firstName = “Pedro”; String lastName = “Penduko”; String course = ”Computer Science”; String age = “22”; String concatenation String firstName = “Pedro”; String lastName = “Penduko”; String course = ”Computer Science”; String BSCS = “Bachelor of science in “ + course String fullName = firstName + “ “ + lastName; String newString = firstName.concat(“ “).concat(lastName); String Methods String methods in Java are built-in functions that make it easier to manipulate and work with strings. Commonly used String Method:.length().toLowerCase().charAt().concat().equals().replace().equalsIgnoreCase().trim().startWith().indexOf().endWith().toUpperCase() String Methods The.length() method returns the length of the string. ex. String course = "Computer Science"; System.out.println(course.length()); //output: 16 The.charAt() method returns the character at the specified index. ex. String course = "Computer Science"; System.out.println(course.charAt(5)); //output: t String Methods The.equals() method it compares the strings, it return true if arguments are the same sequence of character (case-sensitive) ex. String string1 = "Hello"; String string2 = "hello"; String string3 = "Hello"; System.out.println(string1.equals(string2)); //Output: false System.out.println(string1.equals(string3)); //Output: true String Methods The.equalsIgnoreCase() method it compares the strings, it return true if arguments are the same sequence of character but ignoring case differences. ex. String string1 = "Hello"; String string2 = "hello"; String string3 = "Hello"; System.out.println(string1.equalsIgnoreCase(string2)); //Output: true String Methods The.startsWith() method checks the string starts with the specified prefix. ex. String string1 = "Hello"; System.out.println(string1.startsWith(“He”)); //Output: true System.out.println(string1.startsWith(“he”)); //Output: false String Methods The.endsWith() method checks the string ends with the specified prefix. ex. String string1 = "Hello"; System.out.println(string1.endsWith(“lo”)); //Output: true System.out.println(string1.endsWith(“llo”)); //Output: true System.out.println(string1.endsWith(“lO”)); //Output: false String Methods The.toUpperCase() method converts all characters in a string to uppercase. ex. String string1 = "heLLo World!"; System.out.println(string1.toUpperCase()); //Output: HELLO WORLD! String Methods The.toLowerCase() method converts all characters in a string to lowercase. ex. String string1 = "heLLo World!"; System.out.println(string1.toLowerCase()); //Output: hello world! String Methods The.trim() method removes whitespace from the beginning and end. ex. String string1 = " hello world! "; System.out.println(string1.trim()); //Output: hello world! String Methods The.indexOf() method searches for the first occurence of a character or substring. ex. String string1 = "hello world!”; System.out.println(string1.indexOf(“llo”)); //Output: 2 System.out.println(string1.indexOf(“abc”)); //Output: -1 String Methods The.concat() method combine the specified string to the end of the first string. ex. String string1 = "hello”; String string2 = “world” System.out.println(string1.concat(string2)); //Output: helloworld String Methods The.concat() method combine the specified string to the end of the first string. ex. String string1 = "hello”; String string2 = “world” System.out.println(string1.concat(“kitty”)); //Output: hellokitty String Methods The.concat() method combine the specified string to the end of the first string. ex. String string1 = "hello”; String string2 = “world” System.out.println(string1.concat(“kitty”)); //Output: hellokitty

Use Quizgecko on...
Browser
Browser