How to remove characters in a string string in C #

2

I have a problem cutting this string string . I get it by scanner, but the scanner pulls out prefixes and suffixes and I want to eliminate them. The code works for me with a string like this ~200|12345678~ and returns 12345678 . But sometimes the scanned code is smaller and the program crashes.

The variable ordr comes from a textbox and the process in a class.

public string limpio()
{

    if (ordr == "")
    {
        MessageBox.Show("Presione OK para cotinuar " + "\n" + "sin digiar Manufacturing Order", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }

    else
    {

        int cadena = ordr;
        if (cadena < 14)
        {
            MessageBox.Show("Debe de contener mas digitos", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        if (cadena >= 14)
        {
            ordr = Convert.ToString(ordr.Substring(5,8));
        }
    }
    return ordr;
}

What I need is that at the time of scanning I remove the prefixes and suffixes that the scanner has by default. An example of a scanned number is ~200|12345678~ . What I need to eliminate is the ~200| of the beginning and the ~ of the end regardless of the amount found between those characters (~ 200 |, ~) .

    
asked by use2105 11.11.2016 в 21:16
source

3 answers

2

You can do it with a single statement using Regex.Replace :

string limpio = Regex.Replace(ordr, @"^~200\|(.*)~$", "$1");

Explanation:

  • ^~200\| looks for ~200| at the beginning of the string.
  • ~$ looks for ~ at the end of the string.
  • (.*) corresponds to any character between the prefix and the suffix.
  • $1 corresponds to the (.*) part of the pattern parameter. That is, it only retains the characters in the middle of the prefix and suffix.

Demo .

    
answered by 11.11.2016 / 22:42
source
1

As I see it, in your question you use Substring(int, int) you can give it a better use if the ~200| of the beginning will always be there, in the following way:

ordr = ordr.Substring(5, ordr.Length - 2);
// El - 2 elimina el ~ del final.

With the length property of the variable ordr if it is a string , you can take only what is after 200.

Another thing, the allocation of string ordr to int cadena , if I'm not wrong, is not valid without a conversion.

If the variable cadena looks for the length of ordr , the correct assignment would be:

int cadena = ordr.Length;

The last detail would be, you do not need to convert to string the result of Substring , in itself it already returns a string .

Having said that, I modified your code for a bit, (just for fun and time without using C #) :

public string limpio() { 
    if (ordr != "") { 
        int cadena = ordr.Length; 
        if (cadena < 14) { 
            MessageBox.Show("Debe de contener mas digitos", "", MessageBoxButtons.OK, MessageBoxIcon.Stop); 
        } 
        if (cadena >= 14) { 
            ordr = ordr.Substring(5, ordr.Length - 2); 
        }
    } 
    else
        MessageBox.Show("Presione OK para cotinuar " + "\n" + "sin digiar Manufacturing Order", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    return ordr; 
}

The reason I changed the order of the conditions was because I thought I would recommend it, but the generated code is not very different, so, there it is.

I hope I helped you! :)

    
answered by 12.11.2016 в 13:46
0

I think that is what you want to do, what you are using is the method Split()

String[] ArrayCadenas = cadena.Split("charADividir")

This will return an array with N String numbers, where N is the number of characters matching the original string + 1;

Example:

String cadena = "~200|12345678~";
String arrCadenas = cadena.Split("|");
foreach(string s in arrCadenas)
{
   Console.WriteLine(s + "\n");
}

Exit:

  

~ 200

     

12345678 ~

Now I think what you want is to do this:

public string clean () {         if (ordr == "")         {             MessageBox.Show ("Press OK to list" + "\ n" + "without digesting Manufacturing Order", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);         }

    else
    {

        int cadena = ordr;
        if (cadena < 14)
        {
            MessageBox.Show("Debe de contener mas digitos", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        if (cadena >= 14)
        {
            string[] arr = ordr.Split(|);

            ordr = Convert.ToString(arr[1].Replace("~", ""));
        }
    }
    return ordr;

}

Note: If there is no match, then there will be only one string that will be the original

    
answered by 11.11.2016 в 22:17