OSDN Git Service

Fixed repo
[mindgames/Mindgames_main.git] / Mindgames / Assets / Aura 2 / Core / Code / Extensions / StringExtensions.cs
1 
2 /***************************************************************************
3 *                                                                          *
4 *  Copyright (c) Raphaël Ernaelsten (@RaphErnaelsten)                      *
5 *  All Rights Reserved.                                                    *
6 *                                                                          *
7 *  NOTICE: Aura 2 is a commercial project.                                 * 
8 *  All information contained herein is, and remains the property of        *
9 *  Raphaël Ernaelsten.                                                     *
10 *  The intellectual and technical concepts contained herein are            *
11 *  proprietary to Raphaël Ernaelsten and are protected by copyright laws.  *
12 *  Dissemination of this information or reproduction of this material      *
13 *  is strictly forbidden.                                                  *
14 *                                                                          *
15 ***************************************************************************/ 
16
17 using System.Text;
18
19 namespace Aura2API
20 {
21     /// <summary>
22     /// Static class containing extension for string type
23     /// </summary>
24     public static class StringExtensions
25     {
26         /// <summary>
27         /// Insert a string before all upper case letters
28         /// </summary>
29         /// <param name="insertedString">String that will be inserted before the upper case letter</param>
30         /// <param name="ignoreFirstLetter">Should the first letter of the string be ignored? Default = true</param>
31         /// <param name="ignoreSpaces">Should insertion be ignored if a space is in front of the upper case letter? Default = true</param>
32         /// <returns>The modified string</returns>
33         public static string InsertStringBeforeUpperCaseLetters(this string sourceString, string insertedString, bool ignoreFirstLetter = true, bool ignoreSpaces = true)
34         {
35             if(string.IsNullOrEmpty(sourceString))
36             {
37                 return "";
38             }
39
40             StringBuilder newText = new StringBuilder(sourceString.Length * 2);
41             if(ignoreFirstLetter)
42             {
43                 newText.Append(sourceString[0]);
44             }
45
46             for(int i = 1; i < sourceString.Length; i++)
47             {
48                 if(char.IsUpper(sourceString[i]) && (sourceString[i - 1] != ' ' || !ignoreSpaces))
49                 {
50                     newText.Append(insertedString);
51                 }
52                 newText.Append(sourceString[i]);
53             }
54
55             return newText.ToString();
56         }
57
58         /// <summary>
59         /// Insert a char before all upper case letters
60         /// </summary>
61         /// <param name="insertedCharacter">Char that will be inserted before the upper case letter</param>
62         /// <param name="ignoreFirstLetter">Should the first letter of the string be ignored? Default = true</param>
63         /// <param name="ignoreSpaces">Should insertion be ignored if a space is in front of the upper case letter? Default = true</param>
64         /// <returns>The modified string</returns>
65         public static string InsertCharacterBeforeUpperCaseLetters(this string sourceString, char insertedCharacter, bool ignoreFirstLetter = true, bool ignoreSpaces = true)
66         {
67             return sourceString.InsertStringBeforeUpperCaseLetters(insertedCharacter.ToString(), ignoreFirstLetter, ignoreSpaces);
68         }
69     }
70 }