Created
November 11, 2020 02:12
-
-
Save wallstop/7c7ce29d72312dbac9d464ffda6eb06a to your computer and use it in GitHub Desktop.
StringExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Assets.Scripts.Core.Serialization; | |
namespace Assets.Scripts.Core.Extension | |
{ | |
public static class StringExtensions | |
{ | |
public static string Center(this string input, int length) | |
{ | |
if (input == null || length <= input.Length) | |
{ | |
return input; | |
} | |
return input.PadLeft((length - input.Length) / 2 + input.Length).PadRight(length); | |
} | |
public static byte [] GetBytes(this string input) | |
{ | |
byte [] bytes = new byte[input.Length * sizeof(char)]; | |
Buffer.BlockCopy(input.ToCharArray(), 0, bytes, 0, bytes.Length); | |
return bytes; | |
} | |
public static string GetString(this byte [] bytes) | |
{ | |
char [] characters = new char[(int) System.Math.Ceiling((double) bytes.Length / sizeof(char))]; | |
Buffer.BlockCopy(bytes, 0, characters, 0, bytes.Length); | |
return new string(characters); | |
} | |
public static string ToJson<T>(this T value) | |
{ | |
return Serializer.JsonStringify(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment