Strings

String is a collection of Characters represented in Java.

Characters is a primitive datatype in Java that represents single ASCII characters trapped within single quotation marks.

// Characters Examples
char myGrade = 'B';
char myVar1 = 65; // Contains A 
char myVar2 = 66; // Contains B
char myVar3 = 67; // Contains C

String is a built-in class in Java where we can create String objects. Objects will also have methods which provides access to String class features.

Indexing a String

Indexing a String is a concept of getting a singular character from the String. Always remember that indexing starts at 0.

/*
Looking at: "Hello!"

 Word  --> |  H  |  e  |  l  |  l  |  o  |  !  |
 Index --> 0     1     2     3     4     5     

*/

String word = "Hello!";
char last = word.charAt(5); // last contains !
char first = word.charAt(0); // first contains H

Size of a String

Since we can access individual characters by accessing the index, it is often nice to know the length/size of a String.

Finding a Character or a Pattern

When you don't know the index of a character or a certain pattern within the String, you can use a indexOf() method to find the index.

Lowercase and Uppercase String

You can convert a String to have all its characters be either lowercase or uppercase.

Last updated