Skip to content

Instantly share code, notes, and snippets.

@EliCDavis
Last active June 14, 2019 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliCDavis/c83dbd8cec7ed3aba5e45a631fa4328a to your computer and use it in GitHub Desktop.
Save EliCDavis/c83dbd8cec7ed3aba5e45a631fa4328a to your computer and use it in GitHub Desktop.
Unity's faster implementations for string operations found: https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity5.html
public static bool CustomEndsWith(string a, string b) {
int ap = a.Length - 1;
int bp = b.Length - 1;
while (ap >= 0 && bp >= 0 && a [ap] == b [bp]) {
ap--;
bp--;
}
return (bp < 0 && a.Length >= b.Length) || (ap < 0 && b.Length >= a.Length);
}
public static bool CustomStartsWith(string a, string b) {
int aLen = a.Length;
int bLen = b.Length;
int ap = 0; int bp = 0;
while (ap < aLen && bp < bLen && a [ap] == b [bp]) {
ap++;
bp++;
}
return (bp == bLen && aLen >= bLen) || (ap == aLen && bLen >= aLen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment