简体   繁体   中英

I'm trying to insert a string into another string without using built-in functions

class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Your string: ");
            string str = Console.ReadLine();

            
            stringFuncs.Insert(str, "Hello", 5);

            
        }
    }

public static string SubString(string word,int a, int b)
        {

            for(int i = a; i < b; i++)
            {
                Console.Write(word[i]);//my substring method
            }
            return word;
        }
public static string Insert(string word, string subs, int index)
        {
            
            int numberOfLetters = 0;
            foreach (var c in word)
            {
                numberOfLetters++;
            }

            
            Console.WriteLine(stringFuncs.SubString(word, 0, index) + subs + stringFuncs.SubString(word, index,numberOfLetters-index));
            return word;

          

        }

I'm getting this as output whenever i write something: My stringHello My string

my substring method is wrong how can i make it so it only takes the part i needed and returns it back?

You can use my method (if you just need a substring method):

Note: if this solution helps you, please mark as answer (don't forget to vote for me).

class Program
{
    static string CustomSubstring(string text, short startIndex, short length)
    {
        short i;
        for (i = startIndex; i < text.Length; i++)
        {
        }
        StringBuilder Temp = new StringBuilder();
        //"StringBuilder.Append()" is faster than "string+=string"
        //switch is faster than if
        switch (length < i)
        {
            case true:
                char[] C = text.ToArray();
                for (short j = startIndex; j <= length; j++)
                {
                    Temp.Append(C[j]);
                }
                break;
        }
        return Temp.ToString();
    }
    static void Main(string[] args)
    {
        Console.WriteLine(CustomSubstring("Merry Christmas", 6, 14));
        Console.ReadKey();
    }
}

Note: if this solution helps you, please mark as answer (don't forget to vote for me).

Output: Christmas

If you want a solution close to yours, with minimal changes, please consider this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main()
        {
            Console.Write("Your string: ");
            string str = Console.ReadLine();                            // Console should be used only in the main part, not the methods
            Console.WriteLine(StringFuncs.Insert(str, "Hello", 5));
            Console.ReadKey();                                          // Waits for the user to press a key
        }
    }

    public class StringFuncs
    {
        /// <summary>
        /// Extracts a part of a string
        /// </summary>
        /// <param name="word"></param> The string you want an extraction from
        /// <param name="startIndex"></param>The position you want tthe extraction to start
        /// <param name="numberOfChars"></param>The number of chars to be extracted
        /// <returns></returns>
        public static string SubString(string word, int startIndex, int numberOfChars)
        {
            string result = "";                         // initialisation of the result string
            for (int i = 0; i < numberOfChars; i++)
            {
                result += word[startIndex + i];         // adding chars one by one 
            }
            return result;                              
        }

        // the method below was detached from your old Insert method as it can be re-used
        /// <summary>
        /// Calculates the number of letter of a string
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static int NumberOfLetters(string word)
        {
            int numberOfLetters = 0;
            foreach (char c in word)    // be explicite, specific and avoid the "var" thing
            {
                numberOfLetters++;
            }
            return numberOfLetters;
        }

        /// <summary>
        /// Inserts a string into another one, at a given position
        /// </summary>
        /// <param name="targetString"></param>The string which will receive the second one
        /// <param name="insertString"></param>The string to be inserted in the first one
        /// <param name="insertPosition"></param>The position of insertion in the first string
        /// <returns></returns>
        public static string Insert(string targetString, string insertString, int insertPosition)
        {
            return SubString(targetString, 0, insertPosition) + insertString + SubString(targetString, insertPosition, NumberOfLetters(targetString) - insertPosition);
        }
    }
}

}

It's not virtuoso-processing-and-speed argued but shows a more pedagogical approach that you can reproduce in other projects. I just prefer readability and understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM