Home

substr

Substr is a string operation that extracts a contiguous portion of a string and returns it as a new string. It is implemented as a function or method in many programming languages and libraries, often under the name substr or a closely related variant. The operation is fundamental for text processing, parsing, and formatting tasks.

Typical usage and parameters vary by language. A common form is substr(string, start, length), where start identifies

Notable language variants include:

- C++: std::string substr(size_t pos = 0, size_t len = npos) with 0-based indexing; pos is the index of

- PHP: substr(string, int start, int length) allows negative start to count from the end and can omit

- R: substr(string, start, stop) uses 1-based indexing and end-inclusive positions.

- SQL: SUBSTRING or SUBSTR functions in many dialects extract a substring with a start position and

- JavaScript: substr is a legacy form that takes (start, length); modern code often uses substring or

Examples vary by language, but the underlying idea remains the same: select a slice of characters from

the
first
character
to
include
and
length
specifies
how
many
characters
to
take.
The
interpretation
of
start
and
length
differs:
many
languages
use
0-based
indexing
(the
first
character
is
at
position
0),
while
others
use
1-based
indexing.
Some
languages
allow
length
to
be
omitted
to
return
the
remainder
of
the
string,
and
some
support
negative
start
values
to
count
from
the
end.
the
first
character,
and
len
is
the
number
of
characters
to
include.
length
to
return
to
the
string
end.
length;
exact
syntax
varies
by
database
system.
slice
instead.
a
string
based
on
position
and
length.
See
also:
substring,
slice,
right,
left.