blob: 5f4a77b0f6a1714712c25c3313b0675b9620fc89 (
plain)
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
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace UVtools.Core.Extensions
{
public static class StringExtensions
{
public static string RemoveFromEnd(this string input, int count)
{
return input.Remove(input.Length - count);
}
/// <summary>
/// Upper the first character in a string
/// </summary>
/// <param name="input">Input string</param>
/// <returns>Modified string with fist character upper</returns>
public static string FirstCharToUpper(this string input)
{
switch (input)
{
case null: throw new ArgumentNullException(nameof(input));
case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
default: return input.First().ToString().ToUpper() + input.Substring(1);
}
}
public static string Repeat(this string s, int n)
=> n <= 0 ? string.Empty : new StringBuilder(s.Length * n).Insert(0, s, n).ToString();
/// <summary>
/// Converts a string into a target type
/// </summary>
/// <typeparam name="T">Target type to convert into</typeparam>
/// <param name="input">Value</param>
/// <returns>Converted value into target type</returns>
public static T Convert<T>(this string input)
{
var converter = TypeDescriptor.GetConverter(typeof(T));
if (converter != null)
{
//Cast ConvertFromString(string text) : object to (T)
return (T)converter.ConvertFromString(input);
}
return default(T);
}
}
}
|