Left, Right, Mid?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Left, Right, Mid?

    What are the equivalant to left, right, etc. in C#. How can I extract
    strings from a string without them? For instance, I have a string like so:

    "c:\thedir\subf older\maybeanot her\__TFMF_lrni ez55ufziugv2nnz nzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0___Select ed.eps"

    I need to return just the directory name "c:\thedir\subf older\maybeanot her\"
    which may be different, and the file name minus the extension and the
    "___Selecte d" portion, so return
    "__TFMF_lrniez5 5ufziugv2nnznze yt_b9c9c1d5-230a-43e1-b38a-7a44_0". I could
    do this in VB.NET as well as TSQL but how in C#? Thank you.

  • DrewCE

    #2
    Re: Left, Right, Mid?

    When dealing with path names use the System.IO.Path object.

    So to get the directory name of some path, like the one in your example,
    just do...

    string dir = System.IO.Path. GetDirectoryNam e(somePath);

    File name minus extension...

    string fn = System.IO.Path. GetFileNameWith outExtension(so mePath);

    Check out the other methods in Path. They are very useful in dealing with
    paths.

    But, to answer your question as to the equivalent of Left, Right, and
    Mid...see string.SubStrin g which can be used in combination with
    string.IndexOf, string.LastInde xOf, etc. string.Split may also be useful to
    you.

    -Drew



    <msnews.microso ft.comwrote in message
    news:ePEX2dTlIH A.5084@TK2MSFTN GP04.phx.gbl...
    What are the equivalant to left, right, etc. in C#. How can I extract
    strings from a string without them? For instance, I have a string like
    so:
    >
    "c:\thedir\subf older\maybeanot her\__TFMF_lrni ez55ufziugv2nnz nzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0___Select ed.eps"
    >
    I need to return just the directory name
    "c:\thedir\subf older\maybeanot her\" which may be different, and the file
    name minus the extension and the "___Selecte d" portion, so return
    "__TFMF_lrniez5 5ufziugv2nnznze yt_b9c9c1d5-230a-43e1-b38a-7a44_0". I could
    do this in VB.NET as well as TSQL but how in C#? Thank you.
    >

    Comment

    • Guest's Avatar

      #3
      Re: Left, Right, Mid?

      Thanks a lot, I've managed to extract path info from the string and even the
      remaining file info, however, I cannot seem to remove the last
      ___Selected.ext part from the file. The ext may be .eps or .jpeg, can vary.
      In sql I'd get charindex of '___' and trim from there but I cant seem to do
      it do it in c#. Any thoughts?


      "DrewCE" <moc.sgodniahc@ werd - backwardswrote in message
      news:6E942535-37F0-400D-995C-07591FBE5505@mi crosoft.com...
      When dealing with path names use the System.IO.Path object.
      >
      So to get the directory name of some path, like the one in your example,
      just do...
      >
      string dir = System.IO.Path. GetDirectoryNam e(somePath);
      >
      File name minus extension...
      >
      string fn = System.IO.Path. GetFileNameWith outExtension(so mePath);
      >
      Check out the other methods in Path. They are very useful in dealing with
      paths.
      >
      But, to answer your question as to the equivalent of Left, Right, and
      Mid...see string.SubStrin g which can be used in combination with
      string.IndexOf, string.LastInde xOf, etc. string.Split may also be useful
      to you.
      >
      -Drew
      >
      >
      >
      <msnews.microso ft.comwrote in message
      news:ePEX2dTlIH A.5084@TK2MSFTN GP04.phx.gbl...
      >What are the equivalant to left, right, etc. in C#. How can I extract
      >strings from a string without them? For instance, I have a string like
      >so:
      >>
      >"c:\thedir\sub folder\maybeano ther\__TFMF_lrn iez55ufziugv2nn znzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0___Select ed.eps"
      >>
      >I need to return just the directory name
      >"c:\thedir\sub folder\maybeano ther\" which may be different, and the file
      >name minus the extension and the "___Selecte d" portion, so return
      >"__TFMF_lrniez 55ufziugv2nnznz eyt_b9c9c1d5-230a-43e1-b38a-7a44_0". I
      >could do this in VB.NET as well as TSQL but how in C#? Thank you.
      >>
      >

      Comment

      • DrewCE

        #4
        Re: Left, Right, Mid?

        string somepath = "blahblah___bla h";

        somepath.Remove (s.LastIndexOf( "___"));

        -Drew

        <msnews.microso ft.comwrote in message
        news:eNfGQ%23Tl IHA.1768@TK2MSF TNGP05.phx.gbl. ..
        Thanks a lot, I've managed to extract path info from the string and even
        the remaining file info, however, I cannot seem to remove the last
        ___Selected.ext part from the file. The ext may be .eps or .jpeg, can
        vary. In sql I'd get charindex of '___' and trim from there but I cant
        seem to do it do it in c#. Any thoughts?
        >
        >
        "DrewCE" <moc.sgodniahc@ werd - backwardswrote in message
        news:6E942535-37F0-400D-995C-07591FBE5505@mi crosoft.com...
        >When dealing with path names use the System.IO.Path object.
        >>
        >So to get the directory name of some path, like the one in your example,
        >just do...
        >>
        >string dir = System.IO.Path. GetDirectoryNam e(somePath);
        >>
        >File name minus extension...
        >>
        >string fn = System.IO.Path. GetFileNameWith outExtension(so mePath);
        >>
        >Check out the other methods in Path. They are very useful in dealing
        >with paths.
        >>
        >But, to answer your question as to the equivalent of Left, Right, and
        >Mid...see string.SubStrin g which can be used in combination with
        >string.IndexOf , string.LastInde xOf, etc. string.Split may also be useful
        >to you.
        >>
        >-Drew
        >>
        >>
        >>
        ><msnews.micros oft.comwrote in message
        >news:ePEX2dTlI HA.5084@TK2MSFT NGP04.phx.gbl.. .
        >>What are the equivalant to left, right, etc. in C#. How can I extract
        >>strings from a string without them? For instance, I have a string like
        >>so:
        >>>
        >>"c:\thedir\su bfolder\maybean other\__TFMF_lr niez55ufziugv2n nznzeyt_b9c9c1d 5-230a-43e1-b38a-7a44_0___Select ed.eps"
        >>>
        >>I need to return just the directory name
        >>"c:\thedir\su bfolder\maybean other\" which may be different, and the file
        >>name minus the extension and the "___Selecte d" portion, so return
        >>"__TFMF_lrnie z55ufziugv2nnzn zeyt_b9c9c1d5-230a-43e1-b38a-7a44_0". I
        >>could do this in VB.NET as well as TSQL but how in C#? Thank you.
        >>>
        >>
        >

        Comment

        • Marc Gravell

          #5
          Re: Left, Right, Mid?


          Someting like:
          string foo = "@__TFMF_lrniez 55ufziugv2nnznz eyt_b9c9c1d5-­
          230a-43e1-b38a-7a44_0___Select ed.eps";
          string bar = foo.Substring(0 , foo.LastIndexOf ("___"));

          Marc

          Comment

          • Michael A. Covington

            #6
            Re: Left, Right, Mid?

            I think you are looking for the Substring method.


            Comment

            Working...