C# Xpath concat when single quote(')

CSharp · Console

/Program.cs

using System;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		string originalXpath = "//div[contains(., '{0}')]";
		string inputString = "bjhe'bf83785*^{}^$#%!'edferferf'034**()";
		string output = FormatWithConcat(inputString, originalXpath);
		Console.WriteLine(output); 
		// Output: //div[contains(., concat('bjhe', "'", 'bf83785*^{}^$#%!', "'", 'edferferf', "'", '034**()'))]
	}
	
	private static string FormatWithConcat(string inputString, string originalXpath)
	{
		if (!inputString.Contains('\''))
		{
			return string.Format(originalXpath, inputString);
		}
		
		string replacementString = "concat('" + inputString.Replace("'", "', \"'\", '") + "')"; // Replace single quotes and concatenate with 'concat' function
		string finalString = originalXpath.Replace("'{0}'", replacementString); // Replace the placeholder '{0}' in the original XPath with the replacement string
		return finalString;
	}
}
;