#c# exercise extension methods
12 messages · Page 1 of 1 (latest)
return str.Substring(index + delimiter.Length);
assuming index is say 10 and delimiter is just a ; or whatever - just a character of length 1 - the result of index + delimiter.Length will be what? and thefore how long (.Length) will the result of that substring be?
ok nvm, i misremembered the String.Substring() function. the signature is this https://learn.microsoft.com/de-de/dotnet/api/system.string.substring?view=net-8.0
i thought you have to supply a length in order for it to work but apparently you can either give it a starting positions OR starting position and a length
like "test"Substring(2) results in "st" and "test"Substring(2,1) results in "s"
can you show the testing results for your solution?
yes i can
wait a sec
im trying to do all tasks right now
public static class LogAnalysis
{
// TODO: define the 'SubstringAfter()' extension method on the `string` type
public static string SubstringAfter(this string str, string delimiter)
{
int index = str.IndexOf(delimiter);
if(index == -1){
return str;
}
return str.Substring(index + delimiter.Length);
}
// TODO: define the 'SubstringBetween()' extension method on the `string` type
public static string SubstringBetween(this string str, string start, string end)
{
int startIndex = str.IndexOf(start);
startIndex += start.Length;
int endIndex = str.IndexOf(end, startIndex);
return str.Substring(startIndex, endIndex - startIndex);
}
// TODO: define the 'Message()' extension method on the `string` type
public static string Message(this string str)
{
return str.SubstringAfter(": ");
}
// TODO: define the 'LogLevel()' extension method on the `string` type
public static string LogLevel(this string str)
{
return str.SubstringBetween("[", "]");
}
}
error is wait..
nevermind i got it
there was a stupid misstake by writing