BSSE – 5th Semester Software Construction & Development Fall 2023
SE-305 Software Construction & Development
Lab # 5
Introduction to Strings
Objective/s:
Introduction to Strings
Types of String Functions
Examples
Lab Tasks
1. Introduction to Strings
Strings are used for storing text. A String variable contains a collection of characters surrounded
by double quotes:
String greeting = "Hello";
1.1. String Length
A String in Java is actually an object, which contain methods that can perform certain operations
on strings. For example, the length of a string can be found with the length() method:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " +
txt.length());
1
BSSE – 5th Semester Software Construction & Development Fall 2023
1.2. More String Methods
There are many string methods available, for example toUpperCase() and toLowerCase():
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs "hello
world"
1.3. Finding a Character in a String
The indexOf() method returns the index (the position) of the first occurrence of a specified text
in a string (including whitespace):
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
1.4. All String Methods
The String class has a set of built-in methods that you can use on strings.
Method Description Return Type
charAt() Returns the character at the specified index char
(position)
codePointAt() Returns the Unicode of the character at the int
specified index
codePointBefore() Returns the Unicode of the character before int
the specified index
codePointCount() Returns the number of Unicode values found int
in a string.
2
BSSE – 5th Semester Software Construction & Development Fall 2023
compareTo() Compares two strings lexicographically int
compareToIgnoreCase( Compares two strings lexicographically, int
) ignoring case differences
concat() Appends a string to the end of another string String
contains() Checks whether a string contains a sequence boolean
of characters
contentEquals() Checks whether a string contains the exact boolean
same sequence of characters of the specified
CharSequence or StringBuffer
copyValueOf() Returns a String that represents the characters String
of the character array
endsWith() Checks whether a string ends with the boolean
specified character(s)
equals() Compares two strings. Returns true if the boolean
strings are equal, and false if not
equalsIgnoreCase() Compares two strings, ignoring case boolean
considerations
format() Returns a formatted string using the specified String
locale, format string, and arguments
getBytes() Encodes this String into a sequence of bytes byte[]
using the named charset, storing the result
into a new byte array
getChars() Copies characters from a string to an array of void
chars
hashCode() Returns the hash code of a string int
indexOf() Returns the position of the first found int
occurrence of specified characters in a string
3
BSSE – 5th Semester Software Construction & Development Fall 2023
intern() Returns the canonical representation for the String
string object
isEmpty() Checks whether a string is empty or not boolean
lastIndexOf() Returns the position of the last found int
occurrence of specified characters in a string
length() Returns the length of a specified string int
matches() Searches a string for a match against a regular boolean
expression, and returns the matches
offsetByCodePoints() Returns the index within this String that is int
offset from the given index by
codePointOffset code points
regionMatches() Tests if two string regions are equal boolean
replace() Searches a string for a specified value, and String
returns a new string where the specified
values are replaced
replaceFirst() Replaces the first occurrence of a substring String
that matches the given regular expression
with the given replacement
replaceAll() Replaces each substring of this string that String
matches the given regular expression with the
given replacement
split() Splits a string into an array of substrings String[]
startsWith() Checks whether a string starts with specified boolean
characters
subSequence() Returns a new character sequence that is a CharSequence
subsequence of this sequence
substring() Returns a new string which is the substring of String
4
BSSE – 5th Semester Software Construction & Development Fall 2023
a specified string
toCharArray() Converts this string to a new character array char[]
toLowerCase() Converts a string to lower case letters String
toString() Returns the value of a String object String
toUpperCase() Converts a string to upper case letters String
trim() Removes whitespace from both ends of a String
string
valueOf() Returns the string representation of the String
specified value
2. Tasks
1. Create a class in JAVA implementing all the above explained String functions.