Logo Str

ExtensionsString Module

Extension methods for System.String. Adds extension members on System.String. E.G. .First, .Second, .Last and similar indices. Also adds functionality for negative indices, and s.SliceNeg, s.SliceIdx and s.SliceLooped with an inclusive end index. This module is NOT automatically opened when the namespace Str is opened.

Types

Type Description

StrException

An Exception for the string functions defined in Str

Functions and values

Function or value Description

exnf s

Full Usage: exnf s

Parameters:
    s : string

Returns: string

For string formatting in exceptions. Including surrounding quotes

s : string
Returns: string

Type extensions

Type extension Description

this.First

Full Usage: this.First

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the first character of the string Fails if the string is empty.

Extended Type: String

() : unit
Returns: char

this.Get

Full Usage: this.Get

Parameters:
    index : int

Returns: char
Modifiers: inline

Gets a character at an index, the same as this.[index] or this.Idx(index). Throws a descriptive Exception if the index is out of range. (Use this.GetNeg(i) member if you want to use negative indices too)

Extended Type: String

index : int
Returns: char

this.GetLooped

Full Usage: this.GetLooped

Parameters:
    index : int

Returns: char

Any index returns a value for a non-empty string. The string is treated as an endless loop in both positive and negative directions. Throws StrException if the string is empty.

Extended Type: String

index : int
Returns: char

this.GetNeg

Full Usage: this.GetNeg

Parameters:
    index : int

Returns: char

Gets an item in the string by index. Allows negative indexes too (-1 is the last item, as in Python). (With LangVersion preview, F# also supports indexing from the end with the '^' prefix, e.g. str.[^0] for the last item.)

Extended Type: String

index : int
Returns: char

this.Idx

Full Usage: this.Idx

Parameters:
    index : int

Returns: char
Modifiers: inline

Gets a character at an index, the same as this.[index] or this.Get(index). Throws a descriptive Exception if the index is out of range. (Use this.GetNeg(i) member if you want to use negative indices too)

Extended Type: String

index : int
Returns: char

this.Last

Full Usage: this.Last

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the last character of the string Fails if the string is empty.

Extended Type: String

() : unit
Returns: char

this.LastIndex

Full Usage: this.LastIndex

Parameters:
    () : unit

Returns: int
Modifiers: inline

Returns the last valid index in the string same as: s.Length - 1

Extended Type: String

() : unit
Returns: int

this.LastX

Full Usage: this.LastX

Parameters:
    x : int

Returns: string
Modifiers: inline

Returns the last x characters of the string. x must be between zero and the string length.

Extended Type: String

x : int
Returns: string

this.ReplaceFirst

Full Usage: this.ReplaceFirst

Parameters:
    oldValue : string
    newValue : string

Returns: string

Returns a new string in which only the first occurrence of a specified string in the current instance is replaced with another specified string. (Will return the same instance if text to replace is not found) An empty oldValue is treated as a no-op and returns the input unchanged.

Extended Type: String

oldValue : string
newValue : string
Returns: string

this.ReplaceLast

Full Usage: this.ReplaceLast

Parameters:
    oldValue : string
    newValue : string

Returns: string

Returns a new string in which only the last occurrence of a specified string in the current instance is replaced with another specified string. (Will return the same instance if text to replace is not found) An empty oldValue is treated as a no-op and returns the input unchanged.

Extended Type: String

oldValue : string
newValue : string
Returns: string

this.Second

Full Usage: this.Second

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the second character of the string Fails if the string has fewer than two characters.

Extended Type: String

() : unit
Returns: char

this.SecondLast

Full Usage: this.SecondLast

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the second last character of the string Fails if the string has fewer than two characters.

Extended Type: String

() : unit
Returns: char

this.SliceIdx

Full Usage: this.SliceIdx

Parameters:
    startIdx : int - The inclusive start index of the slice.
    endIdx : int - The inclusive end index of the slice.

Returns: string A new string containing the requested range.

Returns a new string containing the characters between the specified inclusive start and end indices. This member rejects negative and out-of-bounds indices, while the F# slicing notation str.[1..3] does not. To allow negative indices use SliceNeg, to normalize any index with modulo use SliceLooped.

Extended Type: String

startIdx : int

The inclusive start index of the slice.

endIdx : int

The inclusive end index of the slice.

Returns: string

A new string containing the requested range.

StrException Thrown when either index is outside the string or startIdx is greater than endIdx.

this.SliceLooped

Full Usage: this.SliceLooped

Parameters:
    startIdx : int - The inclusive start index to normalize.
    endIdx : int - The inclusive end index to normalize.

Returns: string A new string containing the requested range.

Returns a new string containing the characters between the specified start and end indices after normalizing both indices with modulo. Both indices are inclusive, and negative and out-of-range indices are allowed. If the normalized start index is greater than the normalized end index, an empty string is returned. For an empty input string, an empty string is returned.

Extended Type: String

startIdx : int

The inclusive start index to normalize.

endIdx : int

The inclusive end index to normalize.

Returns: string

A new string containing the requested range.

this.SliceNeg

Full Usage: this.SliceNeg

Parameters:
    startIdx : int - The start index (inclusive, can be negative).
    endIdx : int - The end index (inclusive, can be negative).

Returns: string A new string containing the sliced characters.

Allows for negative indices too. ( -1 is the last character, like Python) The resulting string includes the end index. If the end index is one less than the start index an empty string is returned. For example, str.SliceNeg(0,-3) trims the last two characters from the string. To reject negative indices use SliceIdx, to normalize any index with modulo use SliceLooped. (With LangVersion preview, F# also supports slicing from the end with the '^' prefix, e.g. str.[1..^1] skips the first and last character.)

Extended Type: String

startIdx : int

The start index (inclusive, can be negative).

endIdx : int

The end index (inclusive, can be negative).

Returns: string

A new string containing the sliced characters.

StrException Thrown when either index is out of range or the start index is after the end index.

this.Third

Full Usage: this.Third

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the third character of the string Fails if the string has fewer than three characters.

Extended Type: String

() : unit
Returns: char

this.ThirdLast

Full Usage: this.ThirdLast

Parameters:
    () : unit

Returns: char
Modifiers: inline

Returns the third last character of the string Fails if the string has fewer than three characters.

Extended Type: String

() : unit
Returns: char

Type something to start searching.