Home > OS >  How to Trim Non English characters
How to Trim Non English characters

Time:10-26

I've a string with Spanish language and I need to remove some characters '[', ']' etc,. from it. If i use str.Trim('p', 'P', '[', ']') it is not doing the work. Can someone help me?

stirng str="¿Quiere poner en blanco la pantalla? Presione 1 para la pantalla en blanco, presione 2 para mantener la pantalla. Para cancelar esta operación, presione la tecla cancelar [P200] Presione la tecla inferior derecha para ajustar el volumen [P200] Para repetir las instrucciones, presione la tecla inferior izquierda en el teclado"

CodePudding user response:

If you want to replace [P200] with 200, you can use a Regular Expression to match that pattern specifically and extract the number. \[P(?<num>\d )\] will match [P123] and capture the number in the num named group:

var regex=new Regex(@"\[P(?<num>\d )\]",RegexOptions.IgnoreCase);

var result=regex.Replace(input,"${num}");

This produces

¿Quiere poner en blanco la pantalla? Presione 1 para la pantalla en blanco, presione 2 para mantener la pantalla. Para cancelar esta operación, presione la tecla cancelar 200 ...

If you want to remove the entire match, use "" as the replacement value. The named group isn't needed any more though, so the regular expression can be simplified to \[P\d \] :

var regex=new Regex(@"\[P\d \]",RegexOptions.IgnoreCase);
var result=regex.Replace(input,"");

If you want to remove everything between square brackets, the pattern becomes \[. ?\]. Without ? the pattern would match everything from the first opening bracket to the last closing bracket

CodePudding user response:

String.Trim removes all leading and trailing chars, so at the beginning and end of the string, not between. You could use this extension method:

public static class StringExtensions
{
    public static string RemoveAll(this string s, params char[] removeChars)
    {
        StringBuilder sb = new StringBuilder(s.Length);
        foreach (char c in s)
            if (!removeChars.Contains(c))
                sb.Append(c);
        return sb.ToString();
    }
}

str = str.RemoveAll('p', 'P', '[', ']');

CodePudding user response:

Thanks All! I've found solution for my Requirement.

public static string RemoveBetween(string sourceString, string startTag, string endTag)
    {
        Regex regex = new Regex(string.Format("{0}(.*?){1}", Regex.Escape(startTag), Regex.Escape(endTag)), RegexOptions.RightToLeft);
        return regex.Replace(sourceString, startTag   endTag);
    }

string str = "BienvenidoalcajeroconinstruccionesaudiblesdeNCRBank.[p300]Estecajeroledaráinstruccionesparasuusopormediodelaudífono.[P300]Estecajeroproveeinstruccionesaudiblesqueleayudaránaejecutartransacciones.Eltecladoqueustedvaausarparahacersusseleccionesenelusodelcajeroestasituadoaunaalturaaproximadade3piesdelpiso.Lasteclasestanordenadasenformasimilaraladeunteléfonoconunafilamasdeteclasaladerechadelteclado.Ustedpuedeempezarsutransacciónencualquiermomentoinsertandosutarjetaenellectordetarjetasubicandolaescrituraenrelievedelatarjetahaciaarribayhacialaizquierda.";
string test = RemoveBetween(str, "[", "]"); //convert [p300] -> [];
var removeList = new string[] { "[", "]" };
removeList.ToList().ForEach(o => test = test.Replace(o, "")); // Will remove []
  • Related