View source code
Display the source code in std/string.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

std.string.indexOf - multiple declarations

Function indexOf

Searches for a character in a string or range.

ptrdiff_t indexOf(Range)(
  Range s,
  dchar c,
  CaseSensitive cs = Yes.caseSensitive
)
if (isInputRange!Range && isSomeChar!(ElementType!Range) && !isSomeString!Range);

ptrdiff_t indexOf(C)(
  scope const(C)[] s,
  dchar c,
  CaseSensitive cs = Yes.caseSensitive
)
if (isSomeChar!C);

ptrdiff_t indexOf(Range)(
  Range s,
  dchar c,
  size_t startIdx,
  CaseSensitive cs = Yes.caseSensitive
)
if (isInputRange!Range && isSomeChar!(ElementType!Range) && !isSomeString!Range);

ptrdiff_t indexOf(C)(
  scope const(C)[] s,
  dchar c,
  size_t startIdx,
  CaseSensitive cs = Yes.caseSensitive
)
if (isSomeChar!C);

Parameters

NameDescription
s string or InputRange of characters to search for c in
c character to search for in s
startIdx index to a well-formed code point in s to start searching from; defaults to 0
cs specifies whether comparisons are case-sensitive (Yes.caseSensitive) or not (No.caseSensitive).

Returns

If c is found in s, then the index of its first occurrence is returned. If c is not found or startIdx is greater than or equal to s.length, then -1 is returned. If the parameters are not valid UTF, the result will still be either -1 or in the range [startIdx .. s.length], but will not be reliable otherwise.

Throws

If the sequence starting at startIdx does not represent a well-formed code point, then a UTFException may be thrown.

See Also

countUntil

Example

import std.typecons : No;

string s = "Hello World";
writeln(indexOf(s, 'W')); // 6
writeln(indexOf(s, 'Z')); // -1
writeln(indexOf(s, 'w', No.caseSensitive)); // 6

Example

import std.typecons : No;

string s = "Hello World";
writeln(indexOf(s, 'W', 4)); // 6
writeln(indexOf(s, 'Z', 100)); // -1
writeln(indexOf(s, 'w', 3, No.caseSensitive)); // 6

Function indexOf

Searches for a substring in a string or range.

ptrdiff_t indexOf(Range, Char)(
  Range s,
  const(Char)[] sub
)
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) && isSomeChar!Char);

ptrdiff_t indexOf(Range, Char)(
  Range s,
  const(Char)[] sub,
  in CaseSensitive cs
)
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) && isSomeChar!Char);

ptrdiff_t indexOf(Char1, Char2)(
  const(Char1)[] s,
  const(Char2)[] sub,
  in size_t startIdx
) @safe
if (isSomeChar!Char1 && isSomeChar!Char2);

ptrdiff_t indexOf(Char1, Char2)(
  const(Char1)[] s,
  const(Char2)[] sub,
  in size_t startIdx,
  in CaseSensitive cs
) @safe
if (isSomeChar!Char1 && isSomeChar!Char2);

Parameters

NameDescription
s string or ForwardRange of characters to search for sub in
sub substring to search for in s
startIdx index to a well-formed code point in s to start searching from; defaults to 0
cs specifies whether comparisons are case-sensitive (Yes.caseSensitive) or not (No.caseSensitive)

Returns

The index of the first occurrence of sub in s. If sub is not found or startIdx is greater than or equal to s.length, then -1 is returned. If the arguments are not valid UTF, the result will still be either -1 or in the range [startIdx .. s.length], but will not be reliable otherwise.

Throws

If the sequence starting at startIdx does not represent a well-formed code point, then a UTFException may be thrown.

Bugs

Does not work with case-insensitive strings where the mapping of toLower and toUpper is not 1:1.

Example

import std.typecons : No;

string s = "Hello World";
writeln(indexOf(s, "Wo", 4)); // 6
writeln(indexOf(s, "Zo", 100)); // -1
writeln(indexOf(s, "wo", 3, No.caseSensitive)); // 6

Example

import std.typecons : No;

string s = "Hello World";
writeln(indexOf(s, "Wo")); // 6
writeln(indexOf(s, "Zo")); // -1
writeln(indexOf(s, "wO", No.caseSensitive)); // 6

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis

License

Boost License 1.0.