hi in this lesson we'll take a look at some methods we can use with strings let's go as we learned in the last lesson strings have associated methods that can be used to provide us with information about them in this lesson we'll dive a little deeper into this topic and discuss other useful methods we can use with strings in order to best utilize these methods we must understand how strings are structured we can think of strings as a sequence of characters each character occupies a specific place in that string which is called the index the
letter h is at index zero the second letter l is at index three the index will help us to access different parts of any string each string starts at index 0 and the final index of a string is the number of characters in the string minus 1. we can use the method char at to access a specific character in a string in this case if we write stir dot char at 1 we are asking the program to return the character at that specific index in this case e is the character found at index one let's
take a look at some other string methods we'll start with dot length the dot length method returns the number of characters in a string object next is dot substring which is a little more complicated let's take a look the substring method returns the substring beginning at index from and ending at index two minus one remember strings are immutable this means when you make a string you can't change it so if you call substring and get a shorter string it is a new string the first value given is inclusive meaning it is included in the resulting
substring the last value is exclusive meaning it is not included in the substring this method call would result in a substring with the letters g o and o we can use the parameters index and index 1 to get a single character in the string what value would be printed from this code the letter a is the character found at index 6 so that is what would be printed since the method calls the first index inclusively and the second index exclusively we are really looking at what character or element is at index six if we use
the substring method with only one parameter the substring will start at the given value and continue to the end of the string this is a great example of method overloading the substring method has multiple signatures that allow users to input different parameter values that help improve the functionality of the method as we've seen adding a second int value specifies where the substring should stop and omitting the second value indicates that the substring should run all the way to the end of the string this demonstrates how method overloading can improve the usability of a method so
when we use one parameter of five the substring will start at index five and continue to the end of the string what would happen if we entered a parameter value that is beyond the index positions of the given string a string object has index values from 0 to length minus 1 attempting to access indices outside this range will result in an index out of bounds exception an index out of bounds exception is the computer's way of telling us that something went wrong while trying to access an index because it was out of bounds the next
method we'll look at is dot index of the index of method returns the index of the first occurrence of the given string on the called upon string or up minus 1 if the string does not exist within the called upon string it's important to note that the index of method is case sensitive typing in capital d in this case will yield a result of -1 because the character is not found in this string even though there are two instances of a lowercase d the index value 3 is returned because that is the first index next
we'll look at the dot equal method the equal method will return true if the given string is equal to the one the method is being called upon otherwise false will be returned for strings and other objects we use the equals method to compare whether two values are equivalent we do so because objects cannot be compared using two equal signs we will go into further detail about why that's the case later but for now just know that string other objects rely on the equals method to determine if two values are the same the final method we'll
look at is compare two compare two compares the value of the first character that is not equal in both strings it returns a numerical value based on the comparison because character values are actually numerical values behind the scenes individual characters are actually unicode numbers java uses unicode which assigns a number for every possible character which includes ascii and other characters from languages around the world for a computer to be able to store text and numbers that humans can understand there needs to be a code that transforms characters into numbers the unicode standard defines such a
code by using character encoding the reason character encoding is so important is so that every device can display the same information a custom character encoding scheme might work great on one computer but problems can occur if you send that same information to someone else on a different computer here is another representation of the chart from the slide before each character corresponds to a numerical value and is ordered based on its numerical value the value of each letter increases as we go up the alphabet and uppercase letters have lower numerical values than their lowercase partners the
compareto method calculates the numerical distance between character values let's take a look to see what happens when we compare uppercase d to lowercase d the uppercase d character corresponds with a numerical value of 68. the method takes this numerical value and uses it to calculate the distance from the second string object to compare them the numerical value of the second character is subtracted from the first since the second value lowercase d has a numerical value of 100 the method will use that to compare the two values when 68 is subtracted from 100 the resulting value
is negative 32. this indicates that the original string object is lexicographically lesser than the second string object the negative 32 value indicates that the numerical value of capital d precedes the numerical value of lowercase c by 32 characters each character of both the strings is converted into a unicode value for comparison if both the strings are equal then this method returns a zero otherwise it returns a positive or negative value compare 2 is case sensitive since the ascii table is case sensitive anything after the first differing character will not be compared whether one string has
more or less characters that are equal or not the integer given in the result is based on the difference in value of the characters in the ascii chart now that we've learned about some of the methods it's important that we learn a little bit about where the string class can be found the string class and all of its associated methods are part of a package called java.lang a package is a group of related classes along with their methods fields and constructors packages are part of a programming library we can think of a programming library the
same way that we do a regular library it's a collection of written works created mostly by other people while normal libraries have books programming libraries are made up of packages which are groups of associated classes just as a book has a central theme or thesis a package is generally made up of alike classes that address specific programming problems inside of each package we can find the individual classes that we'd like to use or read the classes themselves are like the pages of a book that are meant to be read interpreted and understood in order to
read each class we need a set of instructions that explain how that class can be used application programming interfaces or apis are a set of instructions that allow programmers to use the class the api gives us instructions that explain what methods like print line and nextint do so that we can utilize them we can use classes and their methods by reading the documentation that is associated with the class api this documentation provides us with an understanding of the methods that we will end up utilizing not all of the methods and instance variables that have been
created to get the class to work documentation for apis and libraries are essential to understanding the attributes and behaviors of an object of a class you can use google to find out about any of the methods and more in the java api and libraries the definitive source for java documentation is oracle a common method found in many class documentations is tostring tostring returns a string representing the class and its values printing an object directly will return the object's hash code not the values associated with it writing a tostring method will override the object's existing tostring
method and return the values we want represented when we print as shown here it's much easier to see what the rectangle object's attributes are when this tostring is used when used with a specific object java recognizes the new tostring and will use that once implemented the string concatenation operator will when given a string operand and a reference convert the reference to a string by invoking the tostring method of the referenced object when we look at the next lesson we will see how to convert primitive types to objects so that we can in turn concatenate those
objects with strings then any type reference values included may be converted to type string by string conversion now that you've learned some string methods let's get some practice using them in the code hs editor