/ src / modules / PowerOCR / PowerOCR / Helpers / StringHelpers.cs
StringHelpers.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Text;
 7  using System.Text.RegularExpressions;
 8  
 9  namespace PowerOCR.Helpers;
10  
11  internal static class StringHelpers
12  {
13      public static string MakeStringSingleLine(this string textToEdit)
14      {
15          if (!textToEdit.Contains('\n')
16              && !textToEdit.Contains('\r'))
17          {
18              return textToEdit;
19          }
20  
21          StringBuilder workingString = new(textToEdit);
22  
23          workingString.Replace("\r\n", " ");
24          workingString.Replace(Environment.NewLine, " ");
25          workingString.Replace('\n', ' ');
26          workingString.Replace('\r', ' ');
27  
28          Regex regex = new("[ ]{2,}");
29          string temp = regex.Replace(workingString.ToString(), " ");
30          workingString.Clear();
31          workingString.Append(temp);
32          if (workingString[0] == ' ')
33          {
34              workingString.Remove(0, 1);
35          }
36  
37          if (workingString[workingString.Length - 1] == ' ')
38          {
39              workingString.Remove(workingString.Length - 1, 1);
40          }
41  
42          return workingString.ToString();
43      }
44  }