Files
UVtools/UVtools.Core/Extensions/StringExtensions.cs
T
Tiago Conceição c230a84290 v2.23.6
- **(Improvement) SL1:** (#314)
   - Complete the SL1 file format with some defaults for convertions
   - Change some data types from bool to byte as in recent prusaslicer changes
- (Upgrade) .NET from 5.0.10 to 5.0.11
- (Upgrade) AvaloniaUI from 0.10.7 to 0.10.8
- (Fix) PWS, PW0, PWM, PWMX, PWMO, PWMS: Incorrect set of layer height for the layer definition when using same positioned layers
2021-10-12 21:38:26 +01:00

59 lines
2.0 KiB
C#

/*
* 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 $"{char.ToUpper(input[0])}{input[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 is not null)
{
//Cast ConvertFromString(string text) : object to (T)
return (T)converter.ConvertFromString(input);
}
return default;
}
}
}