Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Update formatting on strings basics #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions md/fundamental-programming-structures4-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Java does not have a built-in string type. Instead, the standard Java library contains a predefined class called String.

```
```Java
String e = ""; // an empty string
String greeting = "Hello";
```
Expand All @@ -13,7 +13,7 @@ String greeting = "Hello";

You can extract a substring from a larger string with the substring method of the String class.

```
```Java
String greeting = "Hello";
String s = greeting.substring(0, 3);
```
Expand All @@ -26,7 +26,7 @@ The second parameter of substring is the first position that you do not want to

Use + to join (concatenate) two strings.

```
```Java
String expletive = "Expletive"; String PG13 = "deleted";
String message = expletive + PG13;
```
Expand All @@ -35,15 +35,16 @@ String message = expletive + PG13;

When you concatenate a string with a value that is not a string, the latter is converted to a string.

```
```Java
int age = 13;
String rating = "PG" + age;
```

-

```
String all = String.join(" / ", "S", "M", "L", "XL"); // all is the string "S / M / L / XL"
```Java
String all = String.join(" / ", "S", "M", "L", "XL");
// all is the string "S / M / L / XL"
```

-
Expand All @@ -52,7 +53,7 @@ String all = String.join(" / ", "S", "M", "L", "XL"); // all is the string "S /

The String class gives no methods that let you change a character in an existing string.

```
```Java
greeting = greeting.substring(0, 3) + "p!";
```

Expand All @@ -64,7 +65,7 @@ Since you cannot change the individual characters in a Java string, the document

To test whether two strings are equal, use the equals method.

```
```Java
s.equals(t)

"Hello".equals(greeting)
Expand All @@ -90,7 +91,7 @@ or

However, a String variable can also hold a special value, called null

```
```Java
if (str == null)

if (str != null && str.length() != 0)
Expand All @@ -109,7 +110,7 @@ if (str != null && str.length() != 0)

Using the StringBuilder class allows you to build a string from many small pieces.

```
```Java
StringBuilder builder = new StringBuilder();

builder.append(ch); // appends a single character
Expand Down