C# 替换字符串中最后一个点号为逗号
{"title":"C# 替换字符串中最后一个点号为逗号","description":"学习如何在C#中使用 LastIndexOf 和 Substring 方法将字符串中最后一个点号替换为逗号。","keywords":"C#, 字符串, 替换, 点号, 逗号, LastIndexOf, Substring","content":"你可以使用 `LastIndexOf` 方法找到最后一个`.的索引,然后使用 \Substring` 方法截取字符串的前部分和后部分,并将它们拼接起来,再加上`,`。以下是一个示例代码:\n\ncsharp\nstring input = \"Hello.World.\";\nint lastDotIndex = input.LastIndexOf(\"\.");\nif (lastDotIndex >= 0)\n{\n string result = input.Substring(0, lastDotIndex) + \",\" + input.Substring(lastDotIndex + 1);\n Console.WriteLine(result);\n}\nelse\n{\n Console.WriteLine(input);\n}\n\n\n上述代码的输出将会是 `Hello,World.`。"}
原文地址: https://www.cveoy.top/t/topic/pwQZ 著作权归作者所有。请勿转载和采集!