原代码段:
除去后:
/// <summary>
/// 除去代码段中所有注释,关键字,标点符号、运算符,只剩下 类,函数,变量等等、、、。
/// </summary>
/// <param name="sText"></param>
/// <returns></returns>
/// 创建时间: 2022-09-02 最后一次修改时间:2022-09-02
public static string _Syntax_C_Get_WordOnly(this String sText)
{
//第一、除去注释
string sTrueText = sText._Syntax_C_Replace_ExplanatoryNote();
//第二、除去关键字,用空格代替
foreach(string s in Syntax_.C_Keyword)
{
sTrueText = sTrueText._ReplaceWord(s, ""._AddChar(' ',s.Length)); //用 s.Length 个空格替代
}
StringBuilder sbResult = new StringBuilder(sTrueText.Length + 10);
string sRemove = Syntax_.C_OperationalCharacter + "()[]{}";
//第三、除去标点符串,运算符
for (int i = 0; i < sTrueText.Length; i++)
{
if( sRemove.IndexOf(sTrueText[i]) == -1)
{
sbResult.Append(sTrueText[i]);
}
else
{
sbResult.Append(' ');
}
}
/// <summary>
/// 替代单词
/// </summary>
/// <param name="sText"></param>
/// <param name="sFind"></param>
/// <param name="sReplace"></param>
/// <returns></returns>
/// 创建时间: 2022-09-02 最后一次修改时间:2022-09-02
public static string _ReplaceWord(this string sText, string sFind, string sReplace)
{
int nPos = sText._IndexOfWord(sFind, 0);
if (nPos != -1)
{
StringBuilder sbResult = new StringBuilder(sText.Length + sReplace.Length);
int nCopyStart = 0;
while (nPos != -1)
{
for (int i = nCopyStart; i < nPos; ++i)
{
sbResult.Append(sText[i]);
}
sbResult.Append(sReplace);
nCopyStart = nPos + sFind.Length;
nPos = sText._IndexOfWord(sFind, nPos + sFind.Length);
}
//拷贝后面一段
for (int i = nCopyStart; i < sText.Length; ++i)
{
sbResult.Append(sText[i]);
}
return sbResult.ToString();
}
else
{
return sText;
}
}