You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中移除拼接字符串内不必要句号的技术实现问询

字符串处理问题:移除多余句点

我有一组字符串,通过foreach循环为每个字符串末尾添加"."后拼接成单个长字符串,但部分拼接的句号属于多余内容。需要对这个长字符串做如下处理:

  • 当". "后紧跟小写字母时,删除其中的"."
  • 当". "后紧跟大写字母时,保留该"."

我尝试过两种方案但都未成功:

  • 遍历parsedPara的字符检查三连字符组合,但会遗漏三分之二的组合,且无法定位原字符串中对应句号的索引
  • 尝试构造badString = ". " + char.toLower(),但因未知具体字符而无法实现

相关示例代码

public class AnalyzeImage
{
    public async Task analyzeImage(string imageUri)
    {
        string endpoint = Environment.GetEnvironmentVariable("VISION_ENDPOINT");
        string key = Environment.GetEnvironmentVariable("VISION_KEY");

        ImageAnalysisClient client = new ImageAnalysisClient(new Uri(endpoint), new AzureKeyCredential(key));

        ImageAnalysisResult result = client.Analyze(new Uri(imageUri), VisualFeatures.Read, new ImageAnalysisOptions { GenderNeutralCaption = true });

        string cleanLine;
        string parsedPara = string.Empty;
        string miniString = string.Empty;

        int i = 0;
        foreach (DetectedTextBlock block in result.Read.Blocks)
        {
            foreach (DetectedTextLine line in block.Lines)
            {
                cleanLine = line.Text.Replace("'", "");

                if (!cleanLine.EndsWith(".") || !cleanLine.EndsWith(",") || !cleanLine.EndsWith("!") || !cleanLine.EndsWith("?") || !cleanLine.EndsWith("-"))
                {
                    cleanLine += ". "; //为缺少结束符的字符串末尾添加句号
                }
                if (cleanLine.EndsWith(".."))
                {
                    cleanLine.Remove(cleanLine.Length - 1);
                }

                parsedPara += cleanLine; //将字符串拼接成单个长字符串

                foreach (char c in parsedPara) //尝试检查每一组三连字符,找出句中错误放置的句号
                {
                    miniString = miniString + c;

                    if (miniString.Length > 2)
                    {
                        miniString = string.Empty;
                    } else if (miniString.Length == 3) 
                    {
                        char firstChar = miniString[0];
                        char secondChar = miniString[1];
                        char thirdChar = miniString[2];

                        if(firstChar.ToString() == "." && secondChar.ToString() == " " && char.IsLower(thirdChar))
                        {
                            Debug.WriteLine("HIT!");
                        }

                    }
                    i++;
                }
            }
        }
        Debug.WriteLine("parsedPara: " + parsedPara);
    }
}

内容的提问来源于stack exchange,提问作者DMur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:07:22