Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Cleaned up and optimized the string extensions class #301

Merged
merged 2 commits into from
Aug 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions XRTK-Core/Packages/com.xrtk.core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ namespace XRTK.Extensions
/// </summary>
public static class StringExtensions
{
public const string WhiteSpace = " ";

public const string Backslash = "\\";

public const string ForwardSlash = "/";

/// <summary>
/// Encodes the string to base 64 ASCII.
/// </summary>
/// <param name="toEncode">String to encode.</param>
/// <returns>Encoded string.</returns>
public static string EncodeTo64(this string toEncode)
{
byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
return Convert.ToBase64String(toEncodeAsBytes);
return Convert.ToBase64String(Encoding.ASCII.GetBytes(toEncode));
}

/// <summary>
Expand All @@ -29,8 +34,7 @@ public static string EncodeTo64(this string toEncode)
/// <returns>Decoded string.</returns>
public static string DecodeFrom64(this string encodedData)
{
byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
return Encoding.ASCII.GetString(encodedDataAsBytes);
return Encoding.ASCII.GetString(Convert.FromBase64String(encodedData));
}

/// <summary>
Expand All @@ -41,13 +45,13 @@ public static string DecodeFrom64(this string encodedData)
public static string ToProperCase(this string value)
{
// If there are 0 or 1 characters, just return the string.
if (value == null) { return value; }
if (value == null) { return string.Empty; }
if (value.Length < 2) { return value.ToUpper(); }
// If there's already spaces in the string, return.
if (value.Contains(" ")) { return value; }
if (value.Contains(WhiteSpace)) { return value; }

// Start with the first character.
string result = value.Substring(0, 1).ToUpper();
var result = new StringBuilder(value.Substring(0, 1).ToUpper());

// Add the remaining characters.
for (int i = 1; i < value.Length; i++)
Expand All @@ -58,18 +62,18 @@ public static string ToProperCase(this string value)

if (isUpper && !wasLastCharUpper && nextIsLower)
{
result += " ";
result.Append(WhiteSpace);
}

result += value[i];
result.Append(value[i]);

if (isUpper && wasLastCharUpper && !nextIsLower)
{
result += " ";
result.Append(WhiteSpace);
}
}

return result;
return result.ToString();
}

/// <summary>
Expand All @@ -79,7 +83,7 @@ public static string ToProperCase(this string value)
/// <returns></returns>
public static string ToBackSlashes(this string value)
{
return value.Replace("\\", "/");
return value.Replace(Backslash, ForwardSlash);
}

/// <summary>
Expand All @@ -88,7 +92,7 @@ public static string ToBackSlashes(this string value)
/// <param name="value"></param>
public static string ToForwardSlashes(this string value)
{
return value.Replace("/", "\\");
return value.Replace(ForwardSlash, Backslash);
}
}
}