STRINGS BY DANIEL JUSTIN PANI WHAT ARE STRINGS

STRINGS BY: DANIEL, JUSTIN, PANI

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.

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. While, a string literal is a constant. A sequence of fixed characters between quotation marks. For example: String name=“Mooney”; String phonenumber=“ 6474206921”; For example: Label 1. set. Text(“Hello World”);

CONCATENATION It is the combination of a string literal and a string variable. Basically the adding together of strings. For example: String s 1=“World” ← a string variable System. out. print(“Hello”+s 1); a string literal

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)) { label 1. set. Text(“The words match”); } Therefore, the text in label 1 in this case will not become “The words match” as the firstword does not equal the secondword.
![1[variation]) firstword. equals. Ignore. Case(secondword) will ignore the ‘case’(upper or lower) of the letters 1[variation]) firstword. equals. Ignore. Case(secondword) will ignore the ‘case’(upper or lower) of the letters](http://slidetodoc.com/presentation_image_h2/9d8bae107b268db260606110d0520552/image-6.jpg)
1[variation]) firstword. equals. Ignore. Case(secondword) will ignore the ‘case’(upper or lower) of the letters when checking equality. Example: if(firstword. equals. Ignore. Case(secondword)) { label 1. set. Text(“The words match”); } Therefore, the text in label 1 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.

3) firstword. char. At(N) is used to isolate a particular character within a string based on the numeric position of the character. Example: label 1. set. Text(“”+secondword. char. At(0)+secondword. char. At(2)); Therefore, the text in label 1 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: label 1. set. Text(firstword. substring(1, 4)); Therefore, the text in label 1 in this case will become “ELL”.

5) firstword. to. Upper. Case() is used to convert a string of characters to all any specified type of letters (“Upper. Case” could be replaced with “Lower. Case” or “Title. Case”). Example: label 1. set. Text(secondword. to. Upper. Case()); Therefore, the text in label 1 in this case will become “HELLO”. 6) s 1. index. Of(s 2) returns an integer that is the starting position of a substring (s 2) if it occurs (or can be found) within s 1. The returned value is the starting position of that substring (s 2). If s 2 is not within s 1 the returned value is -1. Example: String s 1=“Hello World”; String s 2=“lo”; int location=0; location=s 1. index. Of(s 2); System. out. print(location); Therefore, location will equal to 3 and will display the number 3.

STRING COMMANDS CHART String name = “John Laban”; false name. equals(“Justin Smith”); 10 name. length(); true name. compare. To. Ignore. Case(“john laban”); “john laban” name. to. Lower. Case(); 7 name. index. Of(“an”); “JOHN LABAN” name. to. Upper. Case(); true name. starts. With(“J”); “n La” name. substring(3, 7); name. replace. All(“a”, ”o”); “John Lobon”
- Slides: 9