Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here i have two strings

01/19/2016 110056234 283 4

Jan 11 2016 3:01PM 110056234 283 4

01/19/2016 11234 283 4

I need to get the value(283) from above strings with same code for all. Can any one help me?
Posted
Comments
Navya Sri 28-Jan-16 8:45am    
Some times my string will be 01/19/2016 110056234 this format where the value i need will not be present.
BillWoodruff 28-Jan-16 13:43pm    
Are you getting this data from a database query or some kind of stream ?

That's simple: split the strings using blank as separator and get the last but one item of the resulting array.
 
Share this answer
 
Comments
Navya Sri 28-Jan-16 8:46am    
Some times my string will be 01/19/2016 110056234 this format where the value i need will not be present. In such cases i need to print my value as empty.
Navya Sri 28-Jan-16 8:50am    
what i need exactly is suppose my string is like "01/19/2016 110056234 455 43" where 01/19/2016-date of customer registered.
110056234-Register Id
455-customer Id
43-Customer product code.
Now i need CustomeId from that string wherein some cases customer Id will be missed.
glen205 28-Jan-16 9:52am    
CPallini's solution is still valid - if you split the strings and the resulting array only contains 2 items then you have the case where customerId is missed. If the array has 4 items then the third item is your customerId.

It's probably not great to rely on hard-coding it like this but you've only provided 2 possible use-cases (two strings present and four strings present) so this would be a simple solution....
Matt T Heffron 28-Jan-16 11:57am    
It's not that simple. Note that OP's second example has the date/time in a more "textural" format. There are 3 additional spaces in the date/time there.
So counting the number of space-separated pieces is not going to work in all of the cases!
One way to approach this (other than RegEx) is to write a String Extension method:
C#
public static class StringExtensions
{
    public static string FromNextToLastToLast(this string source, string delimiter)
    {
        int last = source.LastIndexOf(delimiter);
        int nexttolast = source.Substring(0, last).LastIndexOf(delimiter);

        if (last == 0) return String.Empty;

        return source.Substring(nexttolast + 1, last - nexttolast - 1);
    }
}
Test:
C#
List<string> testStrings = new List<string>
{
    " 01/19/2016 110056234",

    "01/19/2016 110056234 283 4",

    "Jan 11 2016 3:01PM 110056234 283 4",

    "01/19/2016 11234 283 4",

    "01/19/2016 110056234 455 43"
};

private void button2_Click(object sender, EventArgs e)
{
    string result;

    foreach (string str in testStrings)
    {
        result = str.FromNextToLastToLast(" ");

        Console.WriteLine("source: \"{0}\" : result: \"{1}\"", 
            str, 
            (result == string.Empty) ? "String.Empty" : result);
    }
}

// results in Console:
source: "01/19/2016 110056234" : result: "String.Empty"
source: "01/19/2016 110056234 283 4" : result: "283"
source: "Jan 11 2016 3:01PM 110056234 283 4" : result: "283"
source: "01/19/2016 11234 283 4" : result: "283"
source: "01/19/2016 110056234 455 43" : result: "455"
 
Share this answer
 
Comments
F-ES Sitecore 29-Jan-16 4:22am    
A few amendments :)

public static string FromNextToLastToLast(this string source, string delimiter)
{
// Trim ensures leading\trailing spaces are ignored
int last = source.Trim().LastIndexOf(delimiter);

// -1 check handles text with no spaces at all
if (last == -1) return String.Empty;

int nexttolast = source.Substring(0, last).LastIndexOf(delimiter);

// you meant "nexttolast == 0", not "last == 0"
if (nexttolast == 0) return String.Empty;

return source.Substring(nexttolast + 1, last - nexttolast - 1);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900