C# - Write a C# console application to implement a string replacement function

编程入门 行业动态 更新时间:2024-10-28 02:21:18

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed 

/*
 * StringReplacement.cs - by Chimomo
 * 
 * Requirements
 * Given three strings: input, oldValue and newValue. Look for oldValue in input and replace it with newValue.
 *
 * Signature:
 * Replace(string input, string oldValue, string newValue);
 *
 * For example:
 * Replace("hello, world", "world", "galaxy") should return "hello, galaxy".
 *
 * Important Note
 * 1. You cannot use built-in string replacement methods such as string.Replace, StringBuilder.Replace, Regex.Replace,
 * etc.
 * 2. You cannot use built-in string search method such as SubString, IndexOf.
 * It is recommended to implement your own SubString, IndexOf methods.
 * 3. C# string is an immutable object. You might want to use char array or StringBuilder to do string manipulations.
 * However, if you use StringBuilder, you can not use StringBuilder's built-in methods to do string replacements and
 * searching (Replace, SubString, IndexOf, Append, Insert, Delete, etc. are not allowed).
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace StringReplacement
{
    public class StringReplacement
    {
        static void Main()
        {
            string s = Replace("Today arare arein what aarae Monday!", "are", "is");
            Console.WriteLine(s);
        }

        /// <summary>
        /// Given three strings: input, oldValue and newValue. Look for oldValue in input and replace it with newValue.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns>The replaced string.</returns>
        static string Replace(string input, string oldValue, string newValue)
        {
            StringBuilder strBuilder = new StringBuilder();
            int inputLen = input.Length;
            int oldValLen = oldValue.Length;
            int newValLen = newValue.Length;
            List<int> posList = SubString(input, oldValue);

            // Found that input has old value, replace it with new value.
            if (posList.Count > 0)
            {
                for (int i = 0; i < inputLen;)
                {
                    if (posList.Contains(i))
                    {
                        for (int j = 0; j < newValLen; j++)
                        {
                            strBuilder.Append(newValue[j]);
                        }

                        i += oldValLen;
                    }
                    else
                    {
                        strBuilder.Append(input[i]);
                        i++;
                    }
                }
            }

            return strBuilder.ToString();
        }

        /// <summary>
        /// To determine if subStr is sub string of str or not.
        /// If it is, add the sub string start position to the return list; otherwise, return null.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="subStr">The sub string.</param>
        /// <returns>The return list which contains the start positions of sub string.</returns>
        static List<int> SubString(string str, string subStr)
        {
            List<int> list = new List<int>();
            int strLen = str.Length;
            int subStrLen = subStr.Length;
            int i = 0;
            int j = 0;
            while (i < strLen)
            {
                if (j >= subStrLen)
                {
                    list.Add(i - subStrLen);
                    j = 0;
                }

                if (str[i].Equals(subStr[j]))
                {
                    i++;
                    j++;
                    continue;
                }

                j = 0;
                if (!str[i].Equals(subStr[j]))
                {
                    i++;
                }
            }

            return list;
        }
    }
}

// Output:
/*
Today aris isin what aarae Monday!

*/

 

更多推荐

C# - Write a C# console application to implement a string replacement function

本文发布于:2023-06-14 03:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1432703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:application   console   Write   implement   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!