forked from dotnet/dev-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
145 lines (127 loc) · 4.61 KB
/
Copy pathStringExtensions.cs
File metadata and controls
145 lines (127 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#pragma warning disable IDE0130
namespace System;
#pragma warning restore IDE0130
internal static class StringExtensions
{
/// <summary>
/// Replaces occurrences of a specified string with another string, starting from a given index.
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The replacement string.</param>
/// <param name="startIndex">The index from which to start replacing.</param>
/// <returns>The modified string after replacements.</returns>
/// <example>
/// <code>
/// "HelloWorldHello".Replace("Hello", "Hi", 5); // returns "HelloWorldHi"
/// </code>
/// </example>
internal static string Replace(this string input, string oldValue, string newValue, int startIndex)
{
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(oldValue))
{
return input;
}
if (startIndex < 0 || startIndex >= input.Length)
{
return input;
}
return input[..startIndex] + input[startIndex..].Replace(oldValue, newValue, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Truncates the string to the specified maximum length.
/// </summary>
/// <param name="input">The input string to truncate.</param>
/// <param name="maxLength">The maximum allowed length.</param>
/// <returns>The truncated string if longer than maxLength; otherwise, the original string.</returns>
/// <example>
/// <code>
/// "HelloWorld".MaxLength(5); // returns "Hello"
/// "Hi".MaxLength(5); // returns "Hi"
/// </code>
/// </example>
internal static string MaxLength(this string input, int maxLength)
{
return input.Length <= maxLength ? input : input[..maxLength];
}
/// <summary>
/// Converts the first character of the string to lowercase (camelCase).
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>The string converted to camelCase.</returns>
/// <example>
/// <code>
/// "HelloWorld".ToCamelCase(); // returns "helloWorld"
/// </code>
/// </example>
internal static string ToCamelCase(this string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
return char.ToLowerInvariant(str[0]) + str[1..];
}
/// <summary>
/// Converts a kebab-case string to camelCase.
/// </summary>
/// <param name="str">The kebab-case input string.</param>
/// <returns>The string converted to camelCase.</returns>
/// <example>
/// <code>
/// "hello-world-example".ToCamelFromKebabCase(); // returns "helloWorldExample"
/// </code>
/// </example>
internal static string ToCamelFromKebabCase(this string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
var parts = str.Split('-');
if (parts.Length == 0)
{
return str.ToCamelCase();
}
return parts[0] + string.Concat(parts.Skip(1).Select(s => char.ToUpperInvariant(s[0]) + s[1..]));
}
/// <summary>
/// Converts the string to kebab-case.
/// </summary>
/// <param name="str">The input string.</param>
/// <returns>The string converted to kebab-case.</returns>
/// <example>
/// <code>
/// "HelloWorld".ToKebabCase(); // returns "hello-world"
/// </code>
/// </example>
internal static string ToKebabCase(this string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "-" + x : x.ToString())).ToLowerInvariant();
}
/// <summary>
/// Converts the first character of the string to uppercase (PascalCase).
/// </summary>
/// <param name="input">The input string.</param>
/// <returns>The string converted to PascalCase.</returns>
/// <example>
/// <code>
/// "helloWorld".ToPascalCase(); // returns "HelloWorld"
/// </code>
/// </example>
internal static string ToPascalCase(this string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
return char.ToUpperInvariant(input[0]) + input[1..];
}
}