Back to Browse

2.7 String Methods | CSAwesome APCSA Java

1.4K views
Apr 8, 2022
57:54

#apcsa #learnjava CSAwesome (course.csawesome.org) is a College Board AP Computer Science A curriculum to teach Java programming. index - A number that represents the position of a character in a string. The first character in a string is at index 0. length - The number of characters in a string. substring - A new string that contains a copy of part of the original string. A String object has index values from 0 to length – 1. Attempting to access indices outside this range will result in an IndexOutOfBoundsException. String objects are immutable, meaning that String methods do not change the String object. Any method that seems to change a string actually creates a new string. The following String methods and constructors, including what they do and when they are used, are part of the AP CS A Java Quick Reference Sheet that you can use during the exam: String(String str) : Constructs a new String object that represents the same sequence of characters as str. int length() : returns the number of characters in a String object. String substring(int from, int to) : returns the substring beginning at index from and ending at index (to -1). String substring(int from) : returns substring(from, length()). int indexOf(String str) : searches for str in the current string and returns the index of the first occurrence of str; returns -1 if not found. boolean equals(String other) : returns true if this (the calling object) is equal to other; returns false otherwise. int compareTo(String other) : returns a value less than 0 if this is less than other; returns zero if this is equal to other; returns a value greater than 0 if this is greater than other. str.substring(index, index + 1) returns a single character at index in string str.

Download

0 formats

No download links available.

2.7 String Methods | CSAwesome APCSA Java | NatokHD