Saturday, July 2, 2016

JAVA String Class



The String Class


String objects contain a sequence of characters plus methods that can operate on the string data

The String Class is called with the format:



objectName.methodName(parameters)




Common String Methods

Combine 2 string elements together and print "combine"

String     value1 = "com";

out.println(value1.concat("bine"));



Extract a substring beginning at the 3rd character and is up to but not including the 7th character to print "stop"

The first character in any string is 0 and counts up from there

String     stringSection = "unstoppable".substring(2,6);

out.println(stringSection);



Return a "True" if the string begins with "in"

"inside".startsWith("in");



Return the # of characters contained in a string

"foreverandever".length();



Returns a position of the beginning of a given string inside of another, returns -1 if it is not found

"where's waldo?".indexOf("waldo");



Create a new string with all lower case letters

"I DON'T KNOW".toLowerCase();



Creates a new string with all Upper Case letters

"happy birthday".toUpperCase();



Returns the character at the given position

"project4".charAt(7);



The Character Class


The type char allows you to store a single character

char uses single quotes 'a', '?', '5'

string use double quotes "hello"

Methods like charAt return a char type output


char     numberPeople = "5 guests".charAt(0);


Each character that can be expressed with a char type also has a number assigned to it called  ASCII/UTF-16 values


'A'  is 65
'B' is 66
'a' is 97
'b' is 98
'*' is 42


Common Character Methods




No comments:

Post a Comment