Loading subject…
substring
A substring is a portion of a string, obtained using an index or slicing operation.
0, so the first character sits at index 0 and the last sits at index length - 1.end - start, which is the arithmetic most substring mistakes come down to.text.upper() on its own changes nothing.| Character | H | e | l | l | o | W | o | r | l | d | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
text[0:5] is "Hello" and not "Hellow".text.charAt(-1) throws a StringIndexOutOfBoundsException where Python would return the last character.length() is a method on a String, and length without brackets is the field on an array.+ operator joins two strings end to end and returns the joined result as a new string."Score: " + 5 produces "Score: 5".str() or inserted with an f-string first."Score: " + 5 raises a TypeError because a string and an integer cannot be added."5" + 1 gives "51" rather than 6, so digits read from input must be converted with Integer.parseInt before any arithmetic.upper() and lower(), Java uses toUpperCase() and toLowerCase().strip() and Java uses trim() to remove whitespace from the two ends of a string." a b " trims to "a b".replace(old, new), and it replaces every occurrence rather than only the first.split(separator), which returns a Python list or a Java array of the pieces and discards the separator itself.raw still holds the original untidy text at the end.indexOf returns -1 when the substring is absent, and so does Python's find.index raises a ValueError instead of returning -1, which is the difference worth remembering."World" in text in Python and text.contains("World") in Java.-1 as a real index and slicing from it reads from the wrong end of the string.if pos: in Python is wrong when the substring is found at index 0, because 0 is falsy, so test if pos != -1: instead.IB-2027-0451 packs three fields into one string, so each field is read out by index.| Variable | Start index | End index | Length | Value |
|---|---|---|---|---|
| board | 0 | 2 (excluded) | 2 | IB |
| year | 3 | 7 (excluded) | 4 | 2027 |
| candidate | 8 | end of string | 4 | 0451 |
int would turn it into 451.end - start before moving on."Hello World"[0:5] give five characters rather than six?strip remove, and what does it leave alone?find and Python's index when the substring is absent?"5" + 1 give "51" in Java but raise an error in Python?text = "Hello World"
first = text[0:5] # "Hello" characters 0,1,2,3,4second = text[6:11] # "World"short = text[6:] # "World" omitting the end runs to the finishlast = text[-1] # "d" negative counts back from the endsize = len(text) # 11String text = "Hello World";
String first = text.substring(0, 5); // "Hello"String second = text.substring(6, 11); // "World"String shortForm = text.substring(6); // "World"char last = text.charAt(text.length() - 1); // 'd'int size = text.length(); // 11name = "Data" + "Science" # "DataScience"score = 5line = "Score: " + str(score) # "Score: 5"line = f"Score: {score}" # "Score: 5", same resultString name = "Data" + "Science"; // "DataScience"int score = 5;String line = "Score: " + score; // "Score: 5"raw = " ada,LOVELACE,1843 "
clean = raw.strip() # "ada,LOVELACE,1843"parts = clean.split(",") # ["ada", "LOVELACE", "1843"]first = parts[0].upper() # "ADA"surname = parts[1].lower() # "lovelace"year = parts[2].replace("18", "19") # "1943"print(first, surname, year) # ADA lovelace 1943String raw = " ada,LOVELACE,1843 ";
String clean = raw.trim(); // "ada,LOVELACE,1843"String[] parts = clean.split(","); // {"ada", "LOVELACE", "1843"}String first = parts[0].toUpperCase(); // "ADA"String surname = parts[1].toLowerCase(); // "lovelace"String year = parts[2].replace("18", "19"); // "1943"System.out.println(first + " " + surname + " " + year);text = "Hello World"
pos = text.find("World") # 6missing = text.find("xyz") # -1# text.index("xyz") # ValueErrorfound = "World" in text # TrueString text = "Hello World";
int pos = text.indexOf("World"); // 6int missing = text.indexOf("xyz"); // -1boolean found = text.contains("World"); // truecode = "IB-2027-0451"
board = code[0:2] # "IB" indices 0 and 1year = code[3:7] # "2027" indices 3,4,5,6candidate = code[8:] # "0451" index 8 to the endyear_number = int(year) # 2027 as an integerprint(board, year_number, candidate)String code = "IB-2027-0451";
String board = code.substring(0, 2); // "IB"String year = code.substring(3, 7); // "2027"String candidate = code.substring(8); // "0451"int yearNumber = Integer.parseInt(year); // 2027System.out.println(board + " " + yearNumber + " " + candidate);