locked
string parsing and split RRS feed

  • Question

  • User-754802328 posted

    Hello all,

    I am parsing and split the string, it splits by '&' character along with space. below is my code. It is working fine on my local pc and also on LIVE server VS(with debugging) but not working on LIVE site. it gives error 'input string was not in correct format' when it parse to string TP&WB;-OV.

    I have logged error in error table which logs 'Error while parsing price(3V 1 TP,TP) for Rate Codes; Input string was not in a correct format.' errorlogs display after TP nothing logs.

    so, When it split by space then it also split by '&' but I am not sure and why it splits the '&' character.

    parameters

    InsertRateCodes("3V 1 TP&WB;-OV 29.95", 91131);

    function

    private void InsertRateCodes(string rateCodes, int RouteId)
    {
    try
    {
    foreach (var rate_codes_item in rateCodes)
    {

    string[] pieces = rate_codes_item.Split(' ');
    string code_id = string.Empty;
    string caption = string.Empty;
    decimal price = 0;
    int multiplier = 0;
    try
    {
    if (pieces.Length >= 2)
    {
    if (pieces[0].Length <= 2)
    {
    code_id = pieces[0];
    multiplier = Convert.ToInt32(pieces[1]);
    for (int i = 2; i < pieces.Length - 1; i++)
    {
    try
    {
    if (i != 2 && i != pieces.Length - 1)
    {
    pieces[i] = " " + pieces[i];
    }
    caption = caption + Convert.ToString(pieces[i]);
    }
    catch (Exception e)
    {
    strError = "Error while parsing caption(" + rate_codes_item + "," + caption + ") for Rate Codes";
    LogError(e);
    strError = string.Empty;
    }
    }
    try
    {
    price = Convert.ToDecimal(pieces[pieces.Length - 1]);
    }
    catch (Exception e)
    {
    strError = "Error while parsing price(" + rate_codes_item + "," + pieces[pieces.Length - 1] + ") for Rate Codes";
    LogError(e);
    strError = string.Empty;
    }
    }

    }
    }
    catch (Exception e)
    {
    strError = "Error while parsing Rate Codes";
    LogError(e);
    strError = string.Empty;
    }

    }
    }
    catch (Exception e)
    {
    strError = "Error while Insert Rate Codes";
    LogError(e);
    strError = string.Empty;
    }
    }

    Wednesday, May 23, 2018 12:38 PM

All replies

  • User303363814 posted

    This is not actually your code because it will not compile (the .Split(' ') generates a compiler error).  You say that you are splitting the string on '&' and on space but I cannot see where that happens.  However, if I remove the 'for' loop and change the split statement to

    string[] piece = rateCodes.Split(' ');

    Then the code completes with

    code_id = 3V

    multiplier = 1

    caption = TP&WB;-OV

    price = 29.95

    Which looks correct.  Perhaps if you show us actual code that demonstrates the error we could help better?

    BTW - It is vary bad practice to have those try/catch blocks all over the place (it makes your code hard to read and understand).  try/catch should only be used to handle exceptional conditions.  That is things that you don't expect to happen.  Badly formatted data should be expected, it happens all the time.  Use Decimal.TryParse() rather than Convert.ToDecimal

    Thursday, May 24, 2018 1:44 AM
  • User303363814 posted

    FWIW I would put the RateInfo handling code in its own class.  Something like

    public class RateInfo
    {
        public string Code { get; private set; }
        public int Multiplier { get; private set; }
        public string Caption { get; private set; }
        public decimal Rate { get; private set; }
        public string ErrorMessage { get; private set; }
    
        public static bool TryParse(string input, out RateInfo info)
        {
            info = new RateInfo();
    
    // Space is used a separator in the input string, ignore any double spaces var pieces = input.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);

    // There must be at least three pieces in the input - code, multiplier and rate if (pieces.Length < 3) { info.ErrorMessage = "Not enough pieces in the input. Specify code, multiplier and rate"; return false; }
    // A code is either one or two characters if (pieces[0].Length > 2) { info.ErrorMessage = $"Code '{pieces[0]}' is too long - max of two characters"; return false; }
    // The multiplier must be a whole number int multiplier; if (!int.TryParse(pieces[1], out multiplier)) { info.ErrorMessage = $"Multiplier '{pieces[1]}' is not a whole number"; return false; }
    // The rate must be a decimal decimal rate; if (!decimal.TryParse(pieces.Last(), out rate)) { info.ErrorMessage = $"Rate '{pieces.Last()}' is not a valid decimal number"; return false; }

    // Everything is in the correct format
    info.Code = pieces[0];
    info.Multiplier = multiplier;
    info.Caption = string.Join(" ", pieces.Skip(2).Take(pieces.Length - 3)); info.Rate = rate; return true; } public void Save(int routeId) { // Do the save work } }

    Then you call the code with

    RateInfo info;
    var input = "";
    if (!RateInfo.TryParse(input, out info)) {
       // There input string is not in the correct format
       LogError(info.ErrorMessage);
       return;
    }
    
    info.Save(91131);

    Thursday, May 24, 2018 2:21 AM
  • User-330142929 posted

    Hi Kkakadiya@sigmasolve.net,

    Based on your description, I couldn't understand your requirement. 

    foreach (var rate_codes_item in rateCodes)
    {

    string[] pieces = rate_codes_item.Split(' ');

    I found you use split method to split the char parameter "rate_code_item", rather than the string array.

    as far as I know, we couldn't use split function with char variable. This will causes complier error. I guess maybe you create char split function.

    Could you please post the details codes about the split function? If you could post more details information and codes, it will be more easily for us to find the problem.

    Best Regards,

    Abraham

    Thursday, May 24, 2018 3:15 AM
  • User-754802328 posted

    split by space to any string which contains '&', will also consider '&' in string split.

    Thursday, May 24, 2018 7:26 AM
  • User1949625403 posted

    I hope you are getting this problem might be due to you are receiving '&' value in html encode format due to that while splitting the string using split method you are getting error Please check whether you are receiving input as "3V 1 TP&WB;-OV 29.95" or not.

    Thursday, June 21, 2018 2:17 PM