locked
Extracting a substring from a string RRS feed

  • Question

  • Hello, I'm looking to extract the last word in a string from the last space. The problem if the string value is never a set amount of characters and there may be numerous spaces throughout the string.

    A couple of examples: "Title 1 22/11/11" -> Id expect to grab "22/11/11".

    "Title 2 - Title - 01_22/11/11_Test" -> I'd expect to grab "01_22/11/11_Test".

    Any help would be greatly appreciated as I don't have a clue where to start with this :)

    • Edited by siedwards Tuesday, November 22, 2011 1:27 PM
    Tuesday, November 22, 2011 1:26 PM

Answers

  •             string s1 = "Title 1 22/11/11";

                s1 = s1.Substring(s1.LastIndexOf(" ")+1);

                

                string s2 = "Title 2 - Title - 01_22/11/11_Test";

                s2 = s2.Substring(s2.LastIndexOf(" ") + 1);

    • Proposed as answer by Louis.fr Tuesday, November 22, 2011 2:25 PM
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:42 PM
  • One can use regular expression to help parse text such as

    Console.WriteLine( Regex.Match( @"Title 1 22/11/11", @"[^\s]+", RegexOptions.RightToLeft ).Value );
    
    // Outputs 22/11/11
    

    Check out our MSDN .Net Regular expression Forums for specific regular expression questions. Here are some helful links:

    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaMan Tuesday, November 22, 2011 2:58 PM
    • Proposed as answer by JohnGrove Tuesday, November 22, 2011 5:59 PM
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:44 PM
  • Try the following Code

     

                String S = "Title 2 - Title - 01_22/11/11_Test";
                String[] SP = S.Split(new char[] { ' ' });
                String Val = SP[SP.Length-1]; 
    



    --------------------------------------------------------

    Surender Singh Bhadauria

    My Blog

     

    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:35 PM
  • It could be you may need something like so:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String pattern = @"(?:\d{2}_)?\d{2}\/\d{2}\/\d{2}(_\w+)?";
                String[] testWords = { "Title 1 22/11/11", "Title 2 - Title - 01_22/11/11_Test" };
                Regex rx = new Regex(pattern, RegexOptions.Compiled);
                foreach (String word in testWords)            
                    Console.WriteLine(rx.Match(word).Value);         
                Console.ReadLine();
            }
        }
    }
    
    



    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:52 AM
    Tuesday, November 22, 2011 6:14 PM
  • Hi,

    Take a look at the following peice of code

       private string GetData(string InputString)
            {
                String output =string.Empty ;
                try
                {
    
                    if (InputString.Contains (" "))
                    {
                        Char [] s={' '};
                        string[] y = InputString.Split(s, StringSplitOptions.RemoveEmptyEntries );
    
                        if (y.Length > 0)
                        {
                            output = y[y.Length - 1];
                        }
                    }
    
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
                return output;
            }
    

     

    let me know whether it works for you.

    Regards,

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:53 PM

All replies

  • Try the following Code

     

                String S = "Title 2 - Title - 01_22/11/11_Test";
                String[] SP = S.Split(new char[] { ' ' });
                String Val = SP[SP.Length-1]; 
    



    --------------------------------------------------------

    Surender Singh Bhadauria

    My Blog

     

    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:35 PM
  •             string s1 = "Title 1 22/11/11";

                s1 = s1.Substring(s1.LastIndexOf(" ")+1);

                

                string s2 = "Title 2 - Title - 01_22/11/11_Test";

                s2 = s2.Substring(s2.LastIndexOf(" ") + 1);

    • Proposed as answer by Louis.fr Tuesday, November 22, 2011 2:25 PM
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:42 PM
  • One can use regular expression to help parse text such as

    Console.WriteLine( Regex.Match( @"Title 1 22/11/11", @"[^\s]+", RegexOptions.RightToLeft ).Value );
    
    // Outputs 22/11/11
    

    Check out our MSDN .Net Regular expression Forums for specific regular expression questions. Here are some helful links:

    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaMan Tuesday, November 22, 2011 2:58 PM
    • Proposed as answer by JohnGrove Tuesday, November 22, 2011 5:59 PM
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:44 PM
  • Hi,

    Take a look at the following peice of code

       private string GetData(string InputString)
            {
                String output =string.Empty ;
                try
                {
    
                    if (InputString.Contains (" "))
                    {
                        Char [] s={' '};
                        string[] y = InputString.Split(s, StringSplitOptions.RemoveEmptyEntries );
    
                        if (y.Length > 0)
                        {
                            output = y[y.Length - 1];
                        }
                    }
    
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
                return output;
            }
    

     

    let me know whether it works for you.

    Regards,

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:51 AM
    Tuesday, November 22, 2011 1:53 PM
  • Last index of will perform much better than either splitting the string or using regex.  Both of the other two need to parse the entire string, and splitting even needs to copy the entire string an perform multiple pointless memory allocations.
    Tuesday, November 22, 2011 3:53 PM
  • Last index of will perform much better than either splitting the string or using regex.  Both of the other two need to parse the entire string, and splitting even needs to copy the entire string an perform multiple pointless memory allocations.


    Yes the regex will be slower, maybe 10 millseconds, but the way I have written it, it does NOT parse the entire string. It stops upon the first match, moving from the end of the string towards the beginning.

    Unless the OP is doing millions of iterations where memory or time is critical, then yes one needs to think about thoes...but for the most part with systems doing trillions of computations a second, the *it runs slower* argument went out circa 1993.

    I use regex because it is a system which handles string manipulation in the background, so I don't have to do multiple for loops or have to think about handling data. I give it a pattern a viola it gives me the data.

    Just my opinion, I could be wrong. :-)

    I wrote a blog article on the illusion of speed differences here Are C# .Net Regular Expressions Fast Enough for You?


    William Wegerson (www.OmegaCoder.Com)

    • Proposed as answer by JohnGrove Tuesday, November 22, 2011 6:00 PM
    • Unproposed as answer by OmegaMan Tuesday, November 22, 2011 7:46 PM
    • Edited by OmegaMan Tuesday, November 22, 2011 7:54 PM
    Tuesday, November 22, 2011 4:07 PM
  • Well, first off, these types of functions often are used in the context of file manipulation or other places that really do get called a lot.  Readability is also an issue with regex.  Someone who needs to come to these forums to ask a question like this is extremely unlikely to have any proficiency in regular expressions.  This means that the code will be copy/paste with little to no comprehension.  For cases in which built in string functions can do the same job, slightly faster, in about the same amount of code, while being much more understandable to newer programmers makes it the best option.  I agree string manipulation with built in string functions can get lengthy and convoluted when dealing with more complicated requests, and regular expressions can often be much more concise if you're willing to take the time to learn the escape characters.  That simply don't apply to this particular case and audience.
    Tuesday, November 22, 2011 5:02 PM
  •  Someone who needs to come to these forums to ask a question like this is extremely unlikely to have any proficiency in regular expressions.  This means that the code will be copy/paste with little to no comprehension.  
    ...
    That simply don't apply to this particular case and audience.


    I believe that most people who come to these forums are most likely a professional in this field relating to computer science and this is how they make their living. Due to that, since to be a developer already requires an advanced level of knowledge, the person has the intelligence to pick up hard topics and should be showed all altenatives. I would be insulted if someone profiled me like that where they assumed that I couldn't learn hard topics because I was a new poster to a forum and didn't give me all options...that is wrong.

    If I interview someone who laments, "I didn't learn linq extensions; because it was too hard". I would recommend to management to not hire the person and further advise the canidate that they find another profession if its too hard.

    If one see's value in regular expressions or linq or xaml, it is up to them to learn it and yes some will go away with no knowledge; but that is their bad not mine.

    I, and I advise all in these forums, to never hold back demonstrating something because its hard or takes sometime to learn....that is the nature of this job.

    Again that is my opinion and I may be wrong. :-)

     


    William Wegerson (www.OmegaCoder.Com)
    Tuesday, November 22, 2011 5:53 PM
  • I imagine that there is a very significant portion of people posting on these forums that are not professionals in a computer science related field, and quite a few that never will be.  There are a lot of people that explicitly state that they are beginners, and many that ask questions that strongly indicate that they are beginners based on the types of questions that they ask.

    A part of answering questions on a public forum is determining the proficiency of the person asking the question and providing an answer that is understandable to them.  While it would make most every answerer's job easier to assume everyone is an intelligent professional, my experiences have shown that most of them can find the answers to their questions without needing to ask on a forum such as this.

    Tuesday, November 22, 2011 6:11 PM
  • It could be you may need something like so:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String pattern = @"(?:\d{2}_)?\d{2}\/\d{2}\/\d{2}(_\w+)?";
                String[] testWords = { "Title 1 22/11/11", "Title 2 - Title - 01_22/11/11_Test" };
                Regex rx = new Regex(pattern, RegexOptions.Compiled);
                foreach (String word in testWords)            
                    Console.WriteLine(rx.Match(word).Value);         
                Console.ReadLine();
            }
        }
    }
    
    



    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    • Marked as answer by Bob Shen Monday, December 5, 2011 2:52 AM
    Tuesday, November 22, 2011 6:14 PM
  • I'm afraid I beg to differ as to the character or vocation of the clientele.  Many are students looking for help, or beginners trying to get their feet wet.  While I have nothing against providing alternative solutions, I would lay odds that a person unfamiliar with the methods of the String class such that they would ask such a question is more than likely a beginner.

    Given that, I'd suggest not only the simplest solution, but the one that is most optimal.  Your Regex solution is the coding equivalent of swatting a gnat with a sledgehammer.

    Tuesday, November 22, 2011 7:43 PM
  • I'm afraid I beg to differ as to the character or vocation of the clientele.  Many are students looking for help, or beginners trying to get their feet wet.  While I have nothing against providing alternative solutions, I would lay odds that a person unfamiliar with the methods of the String class such that they would ask such a question is more than likely a beginner.

    Given that, I'd suggest not only the simplest solution, but the one that is most optimal.  Your Regex solution is the coding equivalent of swatting a gnat with a sledgehammer.

    Everybody is entitled to their opinion. There is a general misunderstanding however, when it comes to the uses of regex by those who are unfamiliar with it. Because it looks cryptic, some conclude rather hastily, that it should only be used for heavy parsing or difficult situations when in reality as any student of regex knows, that it has wide ranging applicability in common uses and also in difficult situations.

    It's use and power seem to be discouraged quite frequently by those who are generally unfamiliar with it. Yes, basic string manipulation can be done without resorting to a simple regex, but having knowledge of the power of regex can make a vast amount of difference in programming ability and add an arsenal of insight to your skill set and make some tasks significantly easier and far more elegant.

    I think the example William provided is a far cry from swatting a gnat with a sledgehammer as it was a very simplistic example. He is simply providing an alternate method of solving the problem and opening the user's eyes to a world of different possibilities that exist. BTW, regex is generally considered the de facto for real text parsing, and can provide a powerful, flexible, and efficient new way to think about dealing with text. Sometimes I think that by introducing regex in a simple solution can whet a persons palette and encourage its use for the future which will pay off dividends in a programmers career.

    But, anybody can take a paradigm too far whether it be design patterns or regex.


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    • Edited by JohnGrove Tuesday, November 22, 2011 9:00 PM
    Tuesday, November 22, 2011 8:51 PM
  • @CS001 @servy42

    First off the fact that you two invest time in the forums is to be applauded and I don't overlook that fact. :-)

    There is no right or wrong answer for most answers, for many paths can achieve the same result as with this case. The OP is allowed to mark a winner per-se as answer and in some cases any of the replies could have been an answer. My thought is for a forums responder to not fall into that trap of thinking only one way and revel at the differing opoortunities which are out there without prejudice. 

    I get that we all have opinions and work styles and that is what makes this community unique and frankly very valuable. One cannot know everything and having others show how they think and work; makes forums responding a learning experiance for me.

    Regex is a tool which IMHO enhances ones chances of getting to a solution quicker and if I can show that basic patterns can open up a very powerful tool; that is my end goal.

    @John: Very eloquently put. :-)

     


    William Wegerson (www.OmegaCoder.Com)
    Tuesday, November 22, 2011 10:17 PM
  • At risk of belaboring a point, and FSM knows I've gotten into conversations like these before, you know the ones. The ones where two opinions clash, etc. whatever.

    Everybody is entitled to their opinion.

    Precisely correct. I thought I was expressing my opinion. I didn't know it was necessary to be affirmed in my entitlement to that opinion.  But hey, thanks for the validation.

    It's use and power seem to be discouraged quite frequently by those who are generally unfamiliar with it.
    Actually no, and no one is discouraging it here. However, a regex is no place for a beginner who isn't even familiar with the most basic parsing tools available, IndexOf and LastIndexOf. Moreover, it isn't necessary in simple parsing when the standard string parsing tools are available.  Yes, in more complex cases it is invaluable.  There are parsing situations where attempting to parse something using rudimentary tools would be a bloaty near-unworkable mess.  This isn't one of those situations.  A good coder is KISS driven... the principle, not the band... although some like KISS (band) I'm sure. 
    I think the example William provided is a far cry from swatting a gnat with a sledgehammer as it was a very simplistic example.

    The very definition of swatting a gnat (extremely small bug) with a sledgehammer (unnecessarily heavy object).  To present a simplistic example of regex says nothing about it.  But that simplistic example... was a "why this works" provided? No, such a thing is the stuff of entire websites and training materials.  Hence the links.  Servy42 was correct when he/she opined, "This means that the code will be copy/paste with little to no comprehension."  Learning the basics and maybe even the intricacies of an entire new technology just to understand the answer to a simple question?  This is the expectation?  Are you serious?

    But the statements that sent me through the roof...

    If I interview someone who laments, "I didn't learn linq extensions; because it was too hard". I would recommend to management to not hire the person and further advise the canidate that they find another profession if its too hard.

    If one see's value in regular expressions or linq or xaml, it is up to them to learn it and yes some will go away with no knowledge; but that is their bad not mine.

    I, and I advise all in these forums, to never hold back demonstrating something because its hard or takes sometime to learn....that is the nature of this job.

    First, have we established a professional coder?  No, and even if he WAS a professional coder, I wouldn't presume to lecture anyone about what it takes to do the job.  There is quite a lot of presumption going on here.  There is nothing particularly difficult about ANY of these concepts.  But you don't use them because you're aching to use it on something.  You use them because they are sensible and advantageous to use on a case-by-case basis.  KISS.  The right tool for the right job.  And a Regex is precisely NOT needed here because there is another, simpler, more intuitive, and more performance oriented way to do it.
    He is simply providing an alternate method of solving the problem and opening the user's eyes to a world of different possibilities that exist.
    Do you really think that second part happened? Do you think everyone seeks some sort of coding epiphany coming here?  Most of the time, its "I'm just getting started, need help", or "I'm getting this error I can't figure out", or "I'm stuck here."  Person with problem, get them a solution.
    But, anybody can take a paradigm too far whether it be design patterns or regex.

    Here we agree. I've seen people suggest a Regex when IndexOf will suffice. I've seen people struggle with a simple recursive algorithm when they became convinced that a LINQ pattern was necessary. I've seen instances of people diving into lambda expressions when a simple for loop was the only thing required. Coding is KISS principle writ large and using a firehose to water your plants is not KISS.

    Like I said, my opinion.  YMMV.

    Tuesday, November 22, 2011 10:42 PM
  • "A good coder is KISS driven."

    Agreed, however this assumes that regex is difficult when it isn't or that William's example was difficult when it wasn't. As for the rest of what you had said, sounds more like a rant or sermon then anything else. A rather dogmatic rant.

    "And a Regex is precisely NOT needed here because there is another, simpler, more intuitive, and more performance oriented way to do it"

    You assume there is only a single solution to a problem when alternatives abound as well as different ways of thinking. And the performance you are alluding to, has to do with milliseconds, so arguing that the regex was not performance oriented is a stretch. Again, sometimes people like William and others provide a solution as merely a means to expose others to new ways of thinking.

    "Do you think everyone seeks some sort of coding epiphany coming here?"

    Being a contributor or an educator there is always room to impart new ways of performing tasks subtlety with innocuous examples. I take it you just like to argue.

     


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Tuesday, November 22, 2011 10:59 PM
  • No, I don't assume that there is only a single solution.  I have no problem with alternatives, but I'm also not going to advise someone down a path that I disagree with, nor fail to point out that disagreement if others suggest it, especially if the point of that disagreement is a point that can be logically and reasonably argued.  Whether you or anyone else are open to those arguments or not is quite irrelevant to the purpose.

    Was not the LastIndexOf precisely what I said?  Is it not simpler, more intuitive, and more performance oriented (even if you believe that 10ms is not a big amount)?  Why argue that in terms of its irrelevancy then?

    As to the rest, I'll admit, I like an argument, especially if the subject is worthwhile.  Judging by the responses I have received from you, you apparently like to argue as well.


    • Edited by CS001 Tuesday, November 22, 2011 11:17 PM
    Tuesday, November 22, 2011 11:15 PM
  • I have written a blog article on regular expression speed which got its roots from another MSDN help request. Check it out:

    .Net Regex: Can Regular Expression Parsing be Faster than XmlDocument or Linq to Xml? 


    William Wegerson (www.OmegaCoder.Com)
    Friday, November 25, 2011 6:37 PM