将输入字符串中的注释转换为大写
本文关键字:转换 注释 输入 字符串 | 更新日期: 2023-09-27 18:31:19
如何识别用户输入的注释字符串。 假设用户键入了
> I am /*not working*/ right now.
所以我想将注释的子字符串> /* not working*/
转换为大写。 如何在 C# 中做到这一点。 转换不是问题。 问题是如何识别评论? 在 if 块中做什么??
static void comment(string exp_string)
{
for (int i = 0; i < exp_string.Length; i++)
{
if (exp_string[i] == 47 && exp_string[i + 1] == 42)
}
}
您可以使用此方法:
public static string CommentToUpper(string input)
{
int index = input.IndexOf("/*");
if (index >= 0)
{
int endIndex = input.LastIndexOf("*/");
if (endIndex > index)
return string.Format("{0}/*{1}*/{2}",
input.Substring(0, index),
input.Substring(index + 2, endIndex - index - 2).ToUpper(),
input.Substring(endIndex + 2));
else
return string.Format("{0}/*{1}",
input.Substring(0, index),
input.Substring(index + 2).ToUpper());
}
return input;
}
以这种方式使用它:
string output = CommentToUpper("> I am /*not working*/ right now.");
Console.Write(output);
演示
如果您被限制使用像正则表达式这样干净的东西,请考虑使用 String.SubString() 和 String.ToUpper()。
我认为你受到限制是愚蠢的,但有时别无选择。祝你好运。
编辑:要考虑的另一件事是使用IndexOf()。但是,我将其留给您在MSDN中找到