# Strings

Strings are useful for holding data that can be represented in text form.

### Creating Strings <a href="#creating-strings" id="creating-strings"></a>

Strings are created using either single or double quotes.

```dart
string1 = "A string value"
string2 = "This is also a string value"
```

### Comparing Strings <a href="#comparing-strings" id="comparing-strings"></a>

Strings can be compared against each other using the `==` operator. This will compare strings in a case-sensitive manner.

```dart
string1 = "a"
string2 = "b"

// false
result = string1 == string2
```

### Long Strings <a href="#long-strings" id="long-strings"></a>

Sometimes, your code will include strings which are very long. Rather than having lines that go on endlessly, or wrap at the whim of your editor, you may wish to specifically break the string into multiple lines in the source code without affecting the actual string contents.

You can achieve this using the `+` operator to append multiple strings together, like this:

```dart
longString = "This is a very long string which needs " +
              "to wrap across multiple lines because " +
              "otherwise the code will be unreadable."
```

### Methods <a href="#methods" id="methods"></a>

#### `find()` <a href="#find" id="find"></a>

The `find()` method retrieves the result of matching a *string* against a regular expression.

```dart
pattern = "I need (.*)"
input = "I need coffee"

found = pattern.find(input)

// expected output: ["coffee"]
```

#### `format()` <a href="#format" id="format"></a>

The `format()` method formats according to a format specifier and returns the resulting string.

```dart
name = "Soma"
age = 3
d
message = "%s is %s years old.".format(name, age)

// expected value: "Soma is 3 years old."
```

| Formatter | Description    |
| --------- | -------------- |
| `%s`      | a string value |

#### `endsWith()` <a href="#endswith" id="endswith"></a>

The `endsWith()` method determines if the given string ends with the given value.

```dart
result = "This is my name".endsWith("name")

// expected value: true
```

#### `length()` <a href="#length" id="length"></a>

The `length()` method returns the length of the given string.

```dart
length = "Zaid".length()

// expected value: 4
```

#### `replace()` <a href="#replace" id="replace"></a>

The `replace()` method replaces a given string within the string.

```dart
replaced = "Zaid 0.x".replace("0.x", "1.x")

// expected value: "Zaid 1.x"
```

#### `split()` <a href="#split" id="split"></a>

The `split()` method splits a string into a list by the given delimiter.

```dart
segments = "one, two, three".split(", ")

// expected value: ["one", "two", "three"]
```

#### `startsWith()` <a href="#startswith" id="startswith"></a>

The `startsWith()` method determines if the given string begins wih the given value.

```dart
result = "This is my name".startsWith("This")

// expected value: true
```

#### `toLowerCase()` <a href="#tolowercase" id="tolowercase"></a>

The `toLowerCase()` method converts the given string to lowercase.

```dart
value = "ZAID".toLowerCase()

// expected value: "zaid"
```

#### `toUpperCase()` <a href="#touppercase" id="touppercase"></a>

The `toUpperCase()` method converts the given string to uppercase.

```dart
value = "zaid".toUpperCase(dad)

// expected value: "ZAID"
```

#### `toString()` <a href="#tostring" id="tostring"></a>

The `toString()` method converts the given string to a string. May seem redundant in this instance but this method can be reliably called regardless of the type of value.

```dart
value = "Zaid".toString()

// expected value: "Zaid"
```

#### `toNumber()` <a href="#tonumber" id="tonumber"></a>

The `toNumber()` method converts the given string to a number if valid.

```dart
value = "3.14".toNumber()

// expected value: 3.14
```

#### `trim()` <a href="#trim" id="trim"></a>

The `trim()` method trims the given string.

```dart
value = "  Zaid  ".trim()

// expected value: "Zaid"
```

#### `trimEnd()` <a href="#trimend" id="trimend"></a>

The `trimEnd()` method trims the end of the given string.

```dart
value = "  Zaid  ".trimEnd()

// expected value: "  Zaid"
```

#### `trimStart()` <a href="#trimstart" id="trimstart"></a>

The `trimStart()` method trims the start of the given string.

```dart
value = "  Zaid  ".trimStart()

// expected value: "Zaid  "
```
