Full Transcript

Java String Manipulation What are Strings? • A string in literal terms is a series of characters. In technical terms, the basic Java String is basically an array of characters. Why use Strings? • One of the primary functions of modern computer science, is processing human language. • Similarly t...

Java String Manipulation What are Strings? • A string in literal terms is a series of characters. In technical terms, the basic Java String is basically an array of characters. Why use Strings? • One of the primary functions of modern computer science, is processing human language. • Similarly to how numbers are important to math, language symbols are important to meaning and decision making. Although it may not be visible to computer users, computers process language in the background as precisely and accurately as a calculator. Help dialogs provide instructions. Menus provide choices. And data displays show statuses, errors, and real-time changes to the language. • As a Java programmer, one of your main tools for storing and processing language is going to be the String class. String Syntax Examples //String is an array of characters char[] arrSample = {'R', 'O', 'S', 'E'}; String strSample_1 = new String (arrSample); We always cannot write our strings as arrays; hence we can define the String in Java as follows: //Representation of String String strSample_2 = "ROSE"; String Concatenation: Concatenation is joining of two or more strings. //String Concatenation String str1 = "Rock"; String str2 = "Star"; //Method 1 : Using concat String str3 = str1.concat(str2); System.out.println(str3); //Method 2 : Using "+" operator String str4 = str1 + str2; System.out.println(str4); String "Length" Method • Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. String str_Sample = "RockStar"; //Length of a String System.out.println("Length of String: " + str_Sample.length()); String "indexOf" Method • This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur String str_Sample = "RockStar"; System.out.println("Index of character 'S': " + str_Sample.indexOf('S’)); String "charAt" Method • This method returns the character located at the String's specified index. The string indexes start from zero. String str_Sample = "RockStar"; System.out.println("Character at position 5: " + str_Sample.charAt(5)); String "CompareTo" Method • This method compares two strings lexicographically. String str1 = "Strings are immutable"; String str2 = new String("Strings are immutable"); int result = str1.compareTo( str2 ); System.out.println(result); String "Contain" Method • Specify the characters you need to check. String str_Sample = "RockStar"; System.out.println("str_Sample.contains("tar")); String "endsWith" Method • This method tests if this string ends with the specified suffix. String Str = new String("This is really not immutable!!"); boolean retVal; retVal = Str.endsWith( "immutable!!" ); System.out.println("Returned Value = " + retVal ); retVal = Str.endsWith( "immu" ); System.out.println("Returned Value = " + retVal ); String "replace" Method • This method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. String str_Sample = "RockStar"; System.out.println(str_Sample.replace("Rock", "Duke")); String "toUpperCase" Method • This method has two variants. The first variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()). String str_Sample = "RockStar"; //Convert to UpperCase System.out.println(str_Sample.toUpperCase()); String “toLowerCase" Method • This method has two variants. The first variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()). • The second variant takes locale as an argument to be used while converting into lower case. String str_Sample = "RockStar"; System.out.println(str_Sample.toLowerCase());

Use Quizgecko on...
Browser
Browser