Lucene.Net.Support.StringExtensions: Added a TrimEnd() method that can be used on string arrays. This is to mimic Java's Split() method that removes only the null or empty elements from the end of the array that is returned, but leaves any prior empty elements intact.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/6b56e2bd Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/6b56e2bd Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/6b56e2bd Branch: refs/heads/master Commit: 6b56e2bd2448ffa201cb413e812defe9fbadc2b6 Parents: 51a6c52 Author: Shad Storhaug <[email protected]> Authored: Mon Jul 31 12:48:17 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Wed Aug 2 09:53:59 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net/Support/StringExtensions.cs | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6b56e2bd/src/Lucene.Net/Support/StringExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/StringExtensions.cs b/src/Lucene.Net/Support/StringExtensions.cs index 3828a72..b8e4bbd 100644 --- a/src/Lucene.Net/Support/StringExtensions.cs +++ b/src/Lucene.Net/Support/StringExtensions.cs @@ -1,5 +1,6 @@ using Lucene.Net.Util; using System; +using System.Collections.Generic; using System.Text; namespace Lucene.Net.Support @@ -136,5 +137,34 @@ namespace Lucene.Net.Support // codePoint is negative or not found in string return -1; } + + // LUCENENET TODO: BUG Replace all calls to .Split("", StringSplitOptions.RemoveEmptyEntries) + // and Regex.Split("") with .Split("").TrimEnd() and Regex.Split("").TrimEnd() to match the + // behavior of Java's Split() method. + + /// <summary> + /// Removes null or empty elements from the end of a string array. + /// </summary> + /// <param name="input">This string array.</param> + /// <returns>The array with any null or empty elements removed from the end.</returns> + public static string[] TrimEnd(this string[] input) + { + if (input == null) + return null; + int lastElement; + for (lastElement = input.Length - 1; lastElement >= 0; lastElement--) + { + if (!string.IsNullOrEmpty(input[lastElement])) + break; + } + if (lastElement > 0 && lastElement < input.Length) + { + int end = lastElement + 1; + string[] result = new string[end]; + Array.Copy(input, result, end); + return result; + } + return input; + } } } \ No newline at end of file
