5 Replies - 289 Views - Last Post: 08 November 2019 - 09:21 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 292
  • Joined: 25-May 16

How to parse through a String and return value without hard coding

Posted 08 November 2019 - 08:16 AM

Hi everyone, I am trying to download a file as a stream and I am given the filename as part of the http response

As a sample, this is the response from the request that I get as part of the Content-Disposition
attachment; filename=ACH110420191134.pdf; filename*=UTF-8''ACH110420191134.pdf

I am looking to only get the value after filename= and before the ;

This is how I get the information

var byteArray = await response.Content.ReadAsByteArrayAsync();

IEnumerable<string> docInfo;

response.Content.Headers.TryGetValues("Content-Disposition", out docInfo);

string docName = GetFileNameFromString(docInfo.First(), "attachment", "filename*");




The function I created to get the filename is below

private string GetFileNameFromString(string content, string strStart, string strEnd)
        {
            int start;
            int end;
            if (content.Contains(strStart) && content.Contains(strEnd))
            {
                start = content.IndexOf(strStart, 0) + strStart.Length;
                end = content.IndexOf(strEnd, start) - 1;
                return content.Substring(start + 11, end - (start + 12));
            }
            return "";
        }



Any suggestions on how I can keep this to be less hard coded?

Is This A Good Question/Topic? 0
  • +

Replies To: How to parse through a String and return value without hard coding

#2 Ornstein   User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 43
  • Joined: 13-May 15

Re: How to parse through a String and return value without hard coding

Posted 08 November 2019 - 08:51 AM

If you're specifically wondering how best to do what you're trying to do in this given scenario, I'm pretty sure there's a cleaner way to get the value you're interested in. It looks like you're using System.Net.Http - in which case you should be able to get the value you're looking for from the ContentDispositionHeaderValue (e.g. response.Content.Headers.ContentDisposition.FileName). Failing that, check the relevant documentation.

If you really are wondering how to do what you're asking in general, the answer depends on specific circumstances. Sometimes it's OK to take an approach like you've done here. Sometimes you might use regular expressions. Sometimes you might look for a (third party) parser/library/class that can reliably handle whatever format/protocol/etc you're working with.
Was This Post Helpful? 2
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7192
  • View blog
  • Posts: 24,370
  • Joined: 05-May 12

Re: How to parse through a String and return value without hard coding

Posted 08 November 2019 - 09:12 AM

By "hardcoding", I think you actually mean "magic numbers" 11 and 12, and not the hard coded strings "attachment" and "filename*".

There are three approaches:
  • use substrings and pass in a more accurate delimiters, and use the lengths of those delimiters (like you are already doing with using strStart.Length;
  • use regular expressions; or
  • actually parse the Content-Disposition as per grammar defined by RFC6266 using string builders, a dictionary, and some logic.


If all you need is just the value of the filename parameter, and don't really need the other parts, and are interesting in maximum CPU/RAM efficiency, I would suggest continue to use the substring approach.

If you need maximum developer efficiency (and don't mind that now your developers have to understand regular expressions), then the second approach with regular expressions would be best. The CPU/RAM demand will be higher than that of using substrings.

If you will need the other parts of the content disposition, then might as well parse the string instead of doing multiple passes over it pulling out information that you need. In the end, whatever lexer and parser you end up using (even if it's a simple hand coded recursive descent parser), it will likely just use a string builder to build up tokens, and some logic to place the values found in a dictionary. This will be the least efficient in terms of developer time, and will use up even more memory and CPU than the regular expression approach.
Was This Post Helpful? 1
  • +
  • -

#4 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 292
  • Joined: 25-May 16

Re: How to parse through a String and return value without hard coding

Posted 08 November 2019 - 09:17 AM

Yes, I am trying to figure out how to not have the "magic numbers"

Because the size of that string will ultimately change with the name of the file.

I am purely looking to get the value after filename= and before the semicolon ;

so all that would be left is ACH110420191134.pdf

I was thinking of parsing the string by the semicolon since each value ends with it
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7192
  • View blog
  • Posts: 24,370
  • Joined: 05-May 12

Re: How to parse through a String and return value without hard coding

Posted 08 November 2019 - 09:17 AM

As Ornstein pointed out above, the .NET Framework can already do the appropriate RFC compliant parsing for you: ContentDispositionHeaderValue
Was This Post Helpful? 0
  • +
  • -

#6 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 292
  • Joined: 25-May 16

Re: How to parse through a String and return value without hard coding

Posted 08 November 2019 - 09:21 AM

View PostOrnstein, on 08 November 2019 - 08:51 AM, said:

If you're specifically wondering how best to do what you're trying to do in this given scenario, I'm pretty sure there's a cleaner way to get the value you're interested in. It looks like you're using System.Net.Http - in which case you should be able to get the value you're looking for from the ContentDispositionHeaderValue (e.g. response.Content.Headers.ContentDisposition.FileName). Failing that, check the relevant documentation.

If you really are wondering how to do what you're asking in general, the answer depends on specific circumstances. Sometimes it's OK to take an approach like you've done here. Sometimes you might use regular expressions. Sometimes you might look for a (third party) parser/library/class that can reliably handle whatever format/protocol/etc you're working with.


BOOM! That was exactly what I was looking for
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1