C++20 sections · 1024 units
Open in Course

String Length

Measuring string size

Get length with s.length() or equivalently s.size(). Both return character count as unsigned integer. The two methods are identical for strings; use whichever reads better. Length counts characters, not bytes.

For ASCII, characters and bytes match. For Unicode with multi-byte characters, it's more complex, but length() returns character count for common uses. Use length for loop bounds: for (int i = 0; i < s.length(); i++).

Check if string has content before processing. Validate input length against minimum or maximum requirements.