示例#1
0
            /// <summary>
            /// returns a copy of <paramref name="str"/> where the characters
            /// at the specified <paramref name="indices"/> are set to uppercase,
            /// and the rest are set to lowercase.
            /// </summary>
            /// <param name="str"></param>
            /// <param name="indices"></param>
            /// <returns>
            /// a copy of <paramref name="str"/> where the characters
            /// at the specified <paramref name="indices"/> are set to uppercase,
            /// and the rest are set to lowercase
            /// </returns>
            public static string MakeOnlyCharactersAtIndicesUpper(
                this string str,
                params int[] indices)
            {
                // assert that all indices are valid
                for (int i = 0; i < indices.Length; i++)
                {
                    StringUtils.AssertValidIndexInString(
                        indices[i],
                        str,
                        string.Format("indices[{0}]", i)
                        );
                }

                string upperCaseString;

                char[] stringCharArray = str.ToCharArray();

                // turn all characters to lowercase
                for (int i = 0; i < stringCharArray.Length; i++)
                {
                    stringCharArray[i] = char.ToLower(stringCharArray[i]);
                }

                // make characters at specified indices uppercase
                foreach (int index in indices)
                {
                    stringCharArray[index] = char.ToUpper(stringCharArray[index]);
                }

                upperCaseString = new string(stringCharArray);

                return(upperCaseString);
            }
示例#2
0
            /// <summary>
            /// returns whether <paramref name="str"/> starts with specified <paramref name="prefix"/>,
            /// beginning at specified <paramref name="startIndex"/>.
            /// </summary>
            /// <param name="str"></param>
            /// <param name="startIndex"></param>
            /// <param name="prefix"></param>
            /// <returns>
            /// true if <paramref name="str"/> starts with specified <paramref name="prefix"/>,
            /// beginning at specified <paramref name="startIndex"/>,
            /// else false
            /// </returns>
            /// <exception cref="IndexOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidIndexInString(int, string, string)"/>
            /// </exception>
            public static bool StartsWith(this string str, int startIndex, string prefix)
            {
                StringUtils.AssertValidIndexInString(startIndex, str, "startIndex");

                bool startsWithPrefix;

                int curStringIndex;
                int curPrefixIndex;

                // run through str, starting at startIndex, and compare each of its characters
                // with the corresponding character in prefix
                for (curStringIndex = startIndex, curPrefixIndex = 0;
                     curPrefixIndex < prefix.Length && curStringIndex < str.Length;
                     curStringIndex++, curPrefixIndex++)
                {
                    // mismatch between character in str and its corresponding character in prefix
                    if (str[curStringIndex] != prefix[curPrefixIndex])
                    {
                        break;
                    }
                }

                // string starts with prefix (at startIndex)
                // iff end of prefix was reached, i.e all characters of prefix and str match
                startsWithPrefix = curPrefixIndex == prefix.Length;

                return(startsWithPrefix);
            }
示例#3
0
            /// <summary>
            /// returns a copy of <paramref name="str"/> where the characters
            /// at the specified <paramref name="indices"/> are set to uppercase.
            /// </summary>
            /// <param name="str"></param>
            /// <param name="indices"></param>
            /// <returns>
            /// a copy of <paramref name="str"/> where the characters
            /// at the specified <paramref name="indices"/> are set to uppercase.
            /// </returns>
            /// <exception cref="IndexOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidIndexInString(int, string, string)"/>
            /// </exception>
            public static string CharactersAtIndicesToUpper(this string str, params int[] indices)
            {
                // assert that all indices are valid
                for (int i = 0; i < indices.Length; i++)
                {
                    StringUtils.AssertValidIndexInString(
                        indices[i],
                        str,
                        string.Format("indices[{0}]", i)
                        );
                }

                char[] stringCharArray = str.ToCharArray();

                foreach (int index in indices)
                {
                    stringCharArray[index] = char.ToUpper(stringCharArray[index]);
                }

                return(new string(stringCharArray));
            }
示例#4
0
            /// <summary>
            /// returns the index of the first occurrence of the first expression in
            /// specified <paramref name="expressions"/> array which appears in
            /// in <paramref name="str"/>. if no expression appears in <paramref name="str"/> returns -1.
            /// </summary>
            /// <param name="str"></param>
            /// <param name="startIndex"></param>
            /// <param name="expressions"></param>
            /// <param name="chosenExpression">
            /// the expression which appears first in specified <paramref name="str"/>
            /// </param>
            /// <returns>
            /// index of the first occurrence of the first expression in
            /// specified <paramref name="expressions"/> array which appears in
            /// in <paramref name="str"/>,
            /// or -1 if no expression appears in <paramref name="str"/>
            /// </returns>
            /// <exception cref="IndexOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidIndexInString(int, string, string)"/>
            /// </exception>
            public static int IndexOfAny(
                this string str,
                int startIndex,
                string[] expressions,
                out string chosenExpression)
            {
                StringUtils.AssertValidIndexInString(startIndex, str, "startIndex");

                int indexOfFirstOccurrenceOfChosenExpression = int.MaxValue;

                chosenExpression = null;

                // get the index of the first occurrence of any of the specified expressions
                foreach (string expression in expressions)
                {
                    // get index of first occurrence of expression, starting at startIndex
                    int indexOfNextOccurrenceOfExpressionToOmit = str.IndexOf(
                        expression,
                        startIndex);

                    // first occurrence of expression, starting at startIndex, found in str
                    if (indexOfNextOccurrenceOfExpressionToOmit != -1)
                    {
                        // get the min index of the first expression occurrence between
                        // all specified expressions
                        indexOfFirstOccurrenceOfChosenExpression = Math.Min(
                            indexOfFirstOccurrenceOfChosenExpression,
                            indexOfNextOccurrenceOfExpressionToOmit);
                        chosenExpression = expression;
                    }
                }

                // no occurrence of any of the specified expressions was found in str
                if (indexOfFirstOccurrenceOfChosenExpression == int.MaxValue)
                {
                    indexOfFirstOccurrenceOfChosenExpression = -1;
                }

                return(indexOfFirstOccurrenceOfChosenExpression);
            }