[英]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;
}
每当我写东西时,我都会得到这个 output: My stringHello My string
我的 substring 方法是错误的,我该如何制作它,所以它只需要我需要的部分并将其返回?
您可以使用我的方法(如果您只需要 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 方法):
注意:如果此解决方案对您有帮助,请标记为答案(不要忘记为我投票)。
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();
}
}
注意:如果此解决方案对您有帮助,请标记为答案(不要忘记为我投票)。
Output:圣诞节
如果您想要一个接近您的解决方案,只需进行最少的更改,请考虑以下事项:
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);
}
}
}
}
这不是 virtuoso-processing-and-speed 争论,而是展示了一种更具教学性的方法,您可以在其他项目中复制。 我只是更喜欢可读性和理解力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.