I was looking on the internet for the fastest way of trimming parts of a string. This is the input:
Hello /*test*/World!
This is the outcome I want to achieve:
Hello World!
I tried doing this with String.Remove, but I didn't succeed.
string input = "Hello /*test*/World!";
string output = input;
int index = output.LastIndexOf("/*");
int index2 = output.LastIndexOf("*/");
if (index >= 0)
{
output = output.Remove(index, index2-3);
}
Thank you!
CodePudding user response:
You can try regular expressions:
using System.Text.RegularExpressions;
...
string input = "Hello /*test*/World!";
// we replace each /* ... */ match with empty string
string output = Regex.Replace(input, @"/\*.*?\*/", "");
Pattern /\*.*?\*/
explained:
/\* - /*, note that * is escaped
.*? - any symbols but as few as possible
\*/ - */, note that * is escaped
CodePudding user response:
This is the solution that I made with the help of Steve.
string input = "Hello /*test*/World!";
string output = input;
int index = output.LastIndexOf("/*");
int index2 = output.LastIndexOf("*/");
if (index >= 0)
{
output = output.Remove(index, index2 2 - index);
}