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;
}
每當我寫一些東西時,我都會得到這個作為輸出:我的字串你好我的字串
我的子字串方法是錯誤的,我該怎么做才能讓它只獲取我需要的部分并將其回傳?
uj5u.com熱心網友回復:
您可以使用我的方法(如果您只需要一個子字串方法):
class Program
{
static string CustomSubstring(string text, short startIndex, short indexInQuestion)
{
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 (indexInQuestion < i)
{
case true:
char[] C = text.ToArray();
for (short j = startIndex; j <= indexInQuestion; j )
{
Temp.Append(C[j]);
}
break;
}
return Temp.ToString();
}
static void Main(string[] args)
{
Console.WriteLine(CustomSubstring("Merry Christmas", 6, 14));
Console.ReadKey();
}
}
輸出:圣誕節
uj5u.com熱心網友回復:
如果您想要一個接近您的解決方案,只需進行最少的更改,請考慮以下事項:
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);
}
}
}
這不是對藝術大師處理和速度的爭論,而是展示了一種您可以在其他專案中重現的更具教學性的方法。我只是更喜歡可讀性和理解性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396869.html
上一篇:使用StringBuilder而不是字符陣列旋轉字串
下一篇:洗掉symfony翻譯文本上的%
