sockscape/server/Libraries/Glove/StringExtensions.cs

43 lines
1.4 KiB
C#
Raw Normal View History

2017-05-08 21:06:17 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2017-07-22 19:27:41 +00:00
namespace Glove {
2017-05-08 21:06:17 +00:00
public static class StringExtensions {
2017-05-20 23:33:39 +00:00
public static byte[] GetBytes(this string str, bool isUtf8 = true)
=> isUtf8 ? Encoding.UTF8.GetBytes(str)
: Encoding.ASCII.GetBytes(str);
2017-05-08 21:06:17 +00:00
2017-05-20 23:33:39 +00:00
public static int ByteLength(this string str, bool isUtf8 = true)
=> isUtf8 ? Encoding.UTF8.GetByteCount(str)
: Encoding.ASCII.GetByteCount(str);
2017-05-08 21:06:17 +00:00
public static string Base64Encode(this string str, bool isUtf8 = true) {
var raw =
isUtf8 ? Encoding.UTF8.GetBytes(str)
: Encoding.ASCII.GetBytes(str);
return Convert.ToBase64String(raw);
}
public static string Base64Decode(this string str, bool isUtf8 = true) {
var raw = Convert.FromBase64String(str);
return isUtf8 ? Encoding.UTF8.GetString(raw)
: Encoding.ASCII.GetString(raw);
}
2017-05-20 23:33:39 +00:00
public static byte[] Base64DecodeRaw(this string str)
=> Convert.FromBase64String(str);
2017-06-07 21:02:29 +00:00
public static bool StartsWith(this string str, params string[] strings) {
foreach(var checkString in strings) {
if(str.StartsWith(checkString))
return true;
}
return false;
}
2017-05-08 21:06:17 +00:00
}
}