Download presentation
Presentation is loading. Please wait.
1
STRINGS BY: DANIEL, JUSTIN, PANI
2
WHAT ARE STRINGS? Strings are a sequence of characters, for example; “Hello World”. In java, strings have different variations and can be manipulated using different commands. Commands such as string variables, string literals, string arrays, etc.
3
STRING VARIABLE STRING LITERAL
A string variable is a variable that contains information only with strings in it. Moreover, numbers that are contained in a string variable is recognized by the computer as a string and not an integer. You can use these variables in many ways such as storing information for an input program. For example: String name=“Mooney”; While, a string literal is a constant. A sequence of fixed characters between quotation marks. For example: Label1.setText(“Hello World”); String phonenumber=“ ”;
4
CONCATENATION It is the combination of a string literal and a string variable. Basically the adding together of strings. For example: String s1=“World” ← a string variable System.out.print(“Hello”+s1); a string literal
5
STRING COMMANDS Firstly, let’s declare the string variables used to demonstrate the different commands: String firstword=“HELLO”; String secondword=“hello”; 1) firstword.equals(secondword) is used to check if 2 strings are equal to one another. Example: if(firstword.equals(secondword)) { label1.setText(“The words match”); } Therefore, the text in label1 in this case will not become “The words match” as the firstword does not equal the secondword.
6
1[variation]) firstword
1[variation]) firstword.equalsIgnoreCase(secondword) will ignore the ‘case’(upper or lower) of the letters when checking equality. Example: if(firstword.equalsIgnoreCase(secondword)) { label1.setText(“The words match”); } Therefore, the text in label1 in this case will become “The words match”. 2) firstword.length() is used to determine the length or number of characters in the string. Example: int wordlength=0; wordlength=firstword.length(); Therefore, wordlength in this case will equal to 5 as there are 5 characters stored in the string variable called firstword.
7
3) firstword.charAt(N) is used to isolate a particular character within a string based on the numeric position of the character. Example: label1.setText(“”+secondword.charAt(0)+secondword.charAt(2)); Therefore, the text in label1 in this case will become “hl”. 4) firstword.substring(N,M) is used to isolate and separate a small group of neighbouring characters within a larger string. N is the start position of the group of letters you want to remove and M is the finish position +1. Example: label1.setText(firstword.substring(1,4)); Therefore, the text in label1 in this case will become “ELL”.
8
5) firstword.toUpperCase() is used to convert a string of characters to all any specified type of letters (“UpperCase” could be replaced with “LowerCase” or “TitleCase”). Example: label1.setText(secondword.toUpperCase()); Therefore, the text in label1 in this case will become “HELLO”. 6) s1.indexOf(s2) returns an integer that is the starting position of a substring (s2) if it occurs (or can be found) within s1. The returned value is the starting position of that substring (s2). If s2 is not within s1 the returned value is -1. Example: String s1=“Hello World”; String s2=“lo”; int location=0; location=s1.indexOf(s2); System.out.print(location); Therefore, location will equal to 3 and will display the number 3.
9
String name = “John Laban”;
STRING COMMANDS CHART String name = “John Laban”; name.equals(“Justin Smith”); false name.compareToIgnoreCase(“john laban”); true name.indexOf(“an”); 7 name.startsWith(“J”); name.length(); 10 name.toLowerCase(); “john laban” name.toUpperCase(); “JOHN LABAN” name.substring(3,7); “n La” name. replaceAll(“a”, ”o”); “John Lobon”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.