简体   繁体   中英

Convert Function to C#

I am strugling to find a way to convert this function to C#. could you give me a hand? I have found how to fetch data from the server and get the response using this information here but was not able to find a way to get the indexof and substring the same way this code is doing.

function getIPCA(sm=6, sy=2020, em=12, ey=2020) {
  if (typeof sm === 'number') {
    sm = sm.toString()
  }
  if (typeof em === 'number') {
    em = em.toString()
  }
  sm = sm.length == 1 ? "0"+sm : sm
  em = em.length == 1 ? "0"+em : em
  var options = {
    'method' : 'post',
    //'payload' : 'aba=1&selIndice=00433IPC-A&dataInicial='+sm+'%2F'+sy+'&dataFinal='+em+'%2F'+ey+'&valorCorrecao=1%2C00&idIndice=&nomeIndicePeriodo='
    'payload' : 'aba=1&selIndice=00433IPCA&dataInicial='+sm+'%2F'+sy+'&dataFinal='+em+'%2F'+ey+'&valorCorrecao=1%2C00&idIndice=&nomeIndicePeriodo='
   };

  var response = UrlFetchApp.fetch('https://www3.bcb.gov.br/CALCIDADAO/publico/corrigirPorIndice.do?method=corrigirPorIndice', options)

  var text = response.getContentText();

  var doc = text.substring(text.indexOf('fundoPadraoAClaro3')).substring(978, 987)
  return parseFloat(doc.replace(",","."))

}

I translated the code for you, I think this is what you wanted?

public static float GetIPCA(int sm = 6, int sy = 2020, int em = 12, int ey = 2020)
        {
            string sm_str = sm.ToString();
            string em_str = em.ToString();

            sm_str = sm_str.Length == 1 ? "0" + sm_str : sm_str;
            em_str = em_str.Length == 1 ? "0" + em_str : em_str;

            WebRequest request = WebRequest.Create("https://www3.bcb.gov.br/CALCIDADAO/publico/corrigirPorIndice.do?method=corrigirPorIndice");
            request.Method = "POST";

            string postData = "aba=1&selIndice=00433IPCA&dataInicial="+sm_str+"%2F"+sy+"&dataFinal="+em_str+"%2F"+ey+"&valorCorrecao=1%2C00&idIndice=&nomeIndicePeriodo=";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;

            using (Stream ds = request.GetRequestStream())
            {
                ds.Write(byteArray, 0, byteArray.Length);
            }

            WebResponse response = request.GetResponse();

            string responseFromServer = "";

            using (Stream ds = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(ds))
                {
                    responseFromServer = reader.ReadToEnd();
                }
            }

            response.Close();

            string doc = responseFromServer.Substring(responseFromServer.IndexOf("fundoPadraoAClaro3")).Substring(978, (987-978+1));
            float num = 0;
            float.TryParse(doc.Replace(',','.'), out num);
            return num;
        }

If you have only trouble of converting this line

var doc = text.substring(text.indexOf('fundoPadraoAClaro3')).substring(978, 987)

Below code might help for you

var doc = text.Substring(text.IndexOf("fundoPadraoAClaro3")).Substring(978, 987);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM