Header menu logo TDesu.FSharp

String Module

String utility functions — functional wrappers over String methods.

Functions and values

Function or value Description

String.contains value str

Full Usage: String.contains value str

Parameters:
    value : string - The substring to search for.
    str : string - The string to search in.

Returns: bool
Modifiers: inline

Returns true if str contains value (ordinal). Null-hardened: returns false if either str or value is null, per the "null subject/argument = no match" policy.

value : string

The substring to search for.

str : string

The string to search in.

Returns: bool

String.containsAny comparison values str

Full Usage: String.containsAny comparison values str

Parameters:
    comparison : StringComparison - The string comparison mode to use.
    values : string[] - The substrings to search for.
    str : string - The string to search in.

Returns: bool

Returns true if str contains any of the values, using the given comparison. Null policy matches startsWithAny.

comparison : StringComparison

The string comparison mode to use.

values : string[]

The substrings to search for.

str : string

The string to search in.

Returns: bool

String.containsAnyChar values str

Full Usage: String.containsAnyChar values str

Parameters:
    values : char[] - The characters to search for.
    str : string - The string to search in.

Returns: bool

Returns true if str contains any of the values characters, anywhere in the string. Null policy matches equalsAnyChar.

values : char[]

The characters to search for.

str : string

The string to search in.

Returns: bool

String.countOccurrences substr str

Full Usage: String.countOccurrences substr str

Parameters:
    substr : string - The substring to count.
    str : string - The string to search in.

Returns: int Number of occurrences; 0 for null/empty input or null/empty substr.

Counts the number of non-overlapping occurrences of substr in str. Null-hardened: a nullstr or substr is treated the same as an empty one — the existing empty-input branch already returns 0, so this just extends that branch to null instead of dereferencing .Length on a null reference.

substr : string

The substring to count.

str : string

The string to search in.

Returns: int

Number of occurrences; 0 for null/empty input or null/empty substr.

Example

 "abcabc" |> String.countOccurrences "abc" // 2
module String from Microsoft.FSharp.Core

String.defaultIfEmpty defaultValue str

Full Usage: String.defaultIfEmpty defaultValue str

Parameters:
    defaultValue : string
    str : string

Returns: string
Modifiers: inline

Returns the default value if the string is null or empty. The fallback value. The string to test.

defaultValue : string
str : string
Returns: string

String.endsWith value str

Full Usage: String.endsWith value str

Parameters:
    value : string - The suffix to look for.
    str : string - The string to check.

Returns: bool
Modifiers: inline

Returns true if str ends with value. Null-hardened: returns false if either str or value is null, per the "null subject/argument = no match" policy.

value : string

The suffix to look for.

str : string

The string to check.

Returns: bool

String.endsWithAny comparison values str

Full Usage: String.endsWithAny comparison values str

Parameters:
    comparison : StringComparison - The string comparison mode to use.
    values : string[] - The suffixes to test against.
    str : string - The string to check.

Returns: bool

Returns true if str ends with any of the values, using the given comparison. Null policy matches startsWithAny.

comparison : StringComparison

The string comparison mode to use.

values : string[]

The suffixes to test against.

str : string

The string to check.

Returns: bool

String.endsWithAnyChar values str

Full Usage: String.endsWithAnyChar values str

Parameters:
    values : char[] - The characters to test against.
    str : string - The string to check.

Returns: bool

Returns true if str ends with any of the values characters. Null policy matches equalsAnyChar.

values : char[]

The characters to test against.

str : string

The string to check.

Returns: bool

String.equalsAny comparison values str

Full Usage: String.equalsAny comparison values str

Parameters:
    comparison : StringComparison - The string comparison mode to use.
    values : string[] - The candidates to test against.
    str : string - The string to check.

Returns: bool

Returns true if str is equal to any of the values, using the given comparison.

Null policy matches startsWithAny: null subject/collection never match; a null element of values can equal only a non-null str, so it never spuriously matches a null subject even though Equals(string,string,StringComparison) treats two nulls as equal. comparison is a normal required parameter rather than an overload pair, so the comparison mode is always explicit at the call site.

comparison : StringComparison

The string comparison mode to use.

values : string[]

The candidates to test against.

str : string

The string to check.

Returns: bool

String.equalsAnyChar values str

Full Usage: String.equalsAnyChar values str

Parameters:
    values : char[] - The characters to test against.
    str : string - The string to check.

Returns: bool

Returns true if str, taken as a single character, equals any of the values. A str whose length is not exactly 1 (including empty) never matches, since it cannot represent a single character.

Null policy: a nullstr or values never matches. Unlike the string[] overloads, char is a non-nullable value type, so no element of values can itself be null.

values : char[]

The characters to test against.

str : string

The string to check.

Returns: bool

String.isNotNullOrWhiteSpace

Full Usage: String.isNotNullOrWhiteSpace

Returns: string -> bool

Returns true if string is not null/empty/whitespace.

Returns: string -> bool

String.isNullOrEmpty value

Full Usage: String.isNullOrEmpty value

Parameters:
    value : string

Returns: bool

Returns true if the string is null or empty (no whitespace check).

value : string
Returns: bool

String.isNullOrWhiteSpace value

Full Usage: String.isNullOrWhiteSpace value

Parameters:
    value : string

Returns: bool

Returns true if string is null, empty, or only whitespace.

value : string
Returns: bool

String.join separator values

Full Usage: String.join separator values

Parameters:
    separator : string - The delimiter placed between elements.
    values : string seq - The strings to join.

Returns: string
Modifiers: inline

Joins a sequence of strings with the given separator. Null-hardened: a nullvalues sequence is treated as empty, returning Empty instead of throwing (per the "null collection = empty" policy).

separator : string

The delimiter placed between elements.

values : string seq

The strings to join.

Returns: string

String.padLeft totalWidth str

Full Usage: String.padLeft totalWidth str

Parameters:
    totalWidth : int
    str : string

Returns: string
Modifiers: inline

Pads the string on the left to the given total width. The desired total length after padding. The string to pad.

totalWidth : int
str : string
Returns: string

String.padRight totalWidth str

Full Usage: String.padRight totalWidth str

Parameters:
    totalWidth : int
    str : string

Returns: string
Modifiers: inline

Pads the string on the right to the given total width. The desired total length after padding. The string to pad.

totalWidth : int
str : string
Returns: string

String.remove value str

Full Usage: String.remove value str

Parameters:
    value : string
    str : string

Returns: string
Modifiers: inline

Removes all occurrences of value from the string. The substring to remove. The input string.

value : string
str : string
Returns: string

String.replace oldValue newValue str

Full Usage: String.replace oldValue newValue str

Parameters:
    oldValue : string
    newValue : string
    str : string

Returns: string
Modifiers: inline

Replaces all occurrences of oldValue with newValue. The substring to find. The replacement string. The input string.

oldValue : string
newValue : string
str : string
Returns: string

String.replaceEndLines replacementText str

Full Usage: String.replaceEndLines replacementText str

Parameters:
    replacementText : string - The text to substitute for line endings.
    str : string - The input string.

Returns: string

Replaces all line-ending sequences with the given replacement text. Null-hardened: returns null unchanged if str is null, matching truncate's contract instead of throwing. A nullreplacementText is already handled gracefully by Replace(string,string), which removes the matched line endings instead of substituting them.

replacementText : string

The text to substitute for line endings.

str : string

The input string.

Returns: string

String.split separator str

Full Usage: String.split separator str

Parameters:
    separator : string
    str : string

Returns: string[]
Modifiers: inline

Splits a string by the given separator. The delimiter string. The string to split.

separator : string
str : string
Returns: string[]

String.splitChar separator str

Full Usage: String.splitChar separator str

Parameters:
    separator : char
    str : string

Returns: string[]
Modifiers: inline

Splits a string by the given char separator. The delimiter character. The string to split.

separator : char
str : string
Returns: string[]

String.startsWith value str

Full Usage: String.startsWith value str

Parameters:
    value : string - The prefix to look for.
    str : string - The string to check.

Returns: bool
Modifiers: inline

Returns true if the string starts with the given prefix. Null-hardened: returns false if either str or value is null, per the "null string subject/argument = no match" policy (a null subject never matches).

value : string

The prefix to look for.

str : string

The string to check.

Returns: bool

String.startsWithAny values str

Full Usage: String.startsWithAny values str

Parameters:
    values : string[] - The prefixes to test against.
    str : string - The string to check.

Returns: bool

Returns true if str starts with any of the values.

Null policy: a nullstr never matches (null subject = no match); a null or empty values is treated as an empty set and never matches (null collection = empty); a null element inside values is skipped rather than throwing, since it can never be a prefix of anything.

values : string[]

The prefixes to test against.

str : string

The string to check.

Returns: bool
Example

 "foobar" |> String.startsWithAny [| "foo"; "bar" |] // true
module String from Microsoft.FSharp.Core

String.substring idx str

Full Usage: String.substring idx str

Parameters:
    idx : int
    str : string

Returns: string
Modifiers: inline

Returns the substring starting at the given index. The zero-based start index. The input string.

idx : int
str : string
Returns: string

String.substringLen idx length str

Full Usage: String.substringLen idx length str

Parameters:
    idx : int
    length : int
    str : string

Returns: string
Modifiers: inline

Returns the substring starting at idx with the given length. The zero-based start index. The number of characters to extract. The input string.

idx : int
length : int
str : string
Returns: string

String.toLower str

Full Usage: String.toLower str

Parameters:
    str : string

Returns: string

Converts a string to lowercase using the current culture. The string to convert.

str : string
Returns: string

String.toLowerInv str

Full Usage: String.toLowerInv str

Parameters:
    str : string

Returns: string

Converts a string to lowercase using invariant culture. The string to convert.

str : string
Returns: string

String.toOption s

Full Usage: String.toOption s

Parameters:
    s : string - The string to evaluate.

Returns: string option
Modifiers: inline

Returns None if the string is null/empty/whitespace, otherwise Some(s).

s : string

The string to evaluate.

Returns: string option

String.toUpper str

Full Usage: String.toUpper str

Parameters:
    str : string

Returns: string

Converts a string to uppercase using the current culture. The string to convert.

str : string
Returns: string

String.toUpperInv str

Full Usage: String.toUpperInv str

Parameters:
    str : string

Returns: string

Converts a string to uppercase using invariant culture. The string to convert.

str : string
Returns: string

String.trim str

Full Usage: String.trim str

Parameters:
    str : string

Returns: string
Modifiers: inline

Removes leading and trailing whitespace. The string to trim.

str : string
Returns: string

String.trimEnd str

Full Usage: String.trimEnd str

Parameters:
    str : string

Returns: string
Modifiers: inline

Removes trailing whitespace. The string to trim.

str : string
Returns: string

String.trimStart str

Full Usage: String.trimStart str

Parameters:
    str : string

Returns: string
Modifiers: inline

Removes leading whitespace. The string to trim.

str : string
Returns: string

String.truncate maxLen str

Full Usage: String.truncate maxLen str

Parameters:
    maxLen : int - Maximum length of the returned string.
    str : string - The string to truncate.

Returns: string

Returns at most maxLen characters from the start of the string. Null-safe: returns null if input is null.

maxLen : int

Maximum length of the returned string.

str : string

The string to truncate.

Returns: string
Example

 String.truncate 5 "hello world" // "hello"
module String from Microsoft.FSharp.Core

Type something to start searching.