# Common Tasks

Input text rarely arrives in the format you need, so developers reformat it every day, by trimming, splitting, joining, searching, padding, replacing and reversing.

Apprentice · Java · Updated November 16, 2026

By Gregor Dzierzon

String manipulation is likely an everyday task for most developers. As developers we receive data from many sources: from user input, from a database, from an API, from files or from a variety of other sources. We can't control the format of the input of the strings. However, we usually need to reformat the input so that it matches the format that we require.

The built-in string methods help us perform formatting or processing.

Common string manipulation tasks include:

- Reformat phone numbers to standardize the format
- Trim leading and/or trailing whitespace
- Split lines into parts to store the data
- Join or concatenate strings
- Check if a word or phrase is found in a string
- Padding strings to add leading or trailing spaces or characters
- Remove or replace words or characters
- Reversing strings
- Many more…

## Do it: format a fan's phone number

The team store keeps each fan's phone number as 10 digits with nothing between them, such as `"6145550123"`. Its order slips show the number the way people read it: the area code in parentheses, a space, then the next three digits, a dash, and the last four.

The function gets one value:

- `digits`: the phone number, a `String` of exactly 10 digits (`"6145550123"`).

For example, with `"6145550123"`:

| Piece | Indexes | The text |
|---|---|---|
| The area code | 0 to 2 | `"614"` |
| The next three digits | 3 to 5 | `"555"` |
| The last four digits | 6 to 9 | `"0123"` |
| Joined: `(`, the area code, `) `, the three, `-`, the four | | `"(614) 555-0123"` |

So `formatPhone("6145550123")` returns `"(614) 555-0123"`.

**Your job:** write the function `String formatPhone(String digits)` so it returns the number in that format.

- Put the first three digits in parentheses, then one space.
- Then the next three digits, a dash `-`, and the last four digits.
- Every number has exactly 10 digits, so the pieces are always at the same indexes.

Practiced live on the lesson page: write the Java right there and check it.

Source: https://www.ask-codey.com/lessons/working-with-strings-common-tasks
