# String

## DefaultIfEmpty

Return default value if current string is empty, equals null or equals white space, otherwise current string.

```csharp
var result = "      ".DefaultIfEmpty("defaultValue"); // defaultValue
var result = "someValue".DefaultIfEmpty("defaultValue"); // someValue
```

## Cast

Casts string to specified type or throws if cast is impossible and `throwIfInvalidCast` set to `true`.

```csharp
var result = "123,45".Cast(typeof(float?), true); // 123.45F
var result = "123,45abc".Cast(typeof(float?), true); // throws InvalidCastException
var result = "123,45abc".Cast(typeof(float?), false); // null

private enum FooBarBaz
{
    Foo,
    Bar,
    Baz
}
var result = "Bar".Cast(typeof(FooBarBaz?), false); // FooBarBaz.Bar
```

## ToHyphenCase

Formats string value into hyphen case format. For example "ToHyphenCase" will be formated into "to-hyphen-case".

```csharp
var result = "ToHyphenCase".ToHyphenCase() // to-hyphen-case
```

## ToCamelCase

Formats string value into camel case format. For example "ToCamelCase" will be formated into "toCamelCase".

```csharp
var result = "ToCamelCase".ToCamelCase() // toCamelCase
```

## ToSnakeCase

Formats string value into snake case format. For example "ToSnakeCase" will be formated into "to\_snake\_case".

```csharp
var result = "ToSnakeCase".ToSnakeCase() // to_snake_case
```

## Contains

Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules. Method could be useful to projects prior to .net standard 2.1. Similar method added to `string` in .net standard 2.1.

```csharp
var result = "QwErTy".Contains("wert", StringComparison.Ordinal) // false
var result = "QwErTy".Contains("wert", StringComparison.OrdinalIgnoreCase) // true
```

## Replace

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string using provided comparison rule. Method could be useful to projects prior to .net standard 2.1. Similar method added to `string` in .net standard 2.1.

```csharp
var result = "QwErTyQwErTy".Replace("WeR", "123", StringComparison.Ordinal) // QwErTyQwErTy
var result = "QwErTyQwErTy".Replace("WeR", "123", StringComparison.OrdinalIgnoreCase) // Q123TyQ123Ty
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.structr.dev/utilities/abstractions/extensions/string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
