开发者

C# , Substring How to access last elements of an array/string using substring

开发者 https://www.devze.com 2023-04-10 22:24 出处:网络
I am generating 35 strings which have the names ar15220110910, khwm20110910 and so on. The string contains the name of the Id 开发者_如何学JAVA(ar152,KHWM), and the date (20110910). I want to extrac

I am generating 35 strings which have the names ar15220110910, khwm20110910 and so on.

The string contains the name of the Id 开发者_如何学JAVA(ar152,KHWM), and the date (20110910). I want to extract the Id, date from the string and store it in a textfile called StatSummary.

My code statement is something like this

for( int 1= 0;i< filestoextract.count;1++)
{
    // The filestoextract contains 35 strings
    string extractname = filestoextract(i).ToString();  
    statSummary.writeline( extractname.substring(0,5) + "" + 
         extractname.substring(5,4) + "" + extractname.substring(9,2) + "" + 
         extractname.substring(11,2));
}

When the station has Id containing 5 letters, then this code executes correctly but when the station Id is KHWM or any other 4 letter name then the insertion is all messed up. I am running this inside a loop. So I have tried keeping the code as dynamic as possible. Could anyone help me to find a way without hardcoding it. For instance accessing the last 8 elements to get the date??? I have searched but am not able to find a way to do that.


For the last 8 digits, it's just:

extractname.Substring(extractname.Length-8)

oh, I'm sorry, and so for your code could be:

int l = extractname.Length;
statSummary.WriteLine(extractname.substring(0,l-8) + "" + 
         extractname.Substring(l-8,4) + "" + extractname.Substring(l-4,2) + "" + 
         extractname.Substring(l-2,2));


As your ID length isn't consistent, it would probably be a better option to extract the date (which is always going to be 8 chars) and then treat the remainder as your ID e.g.

UPDATED - more robust by actually calculating the length of the date based on the format. Also validates against the format to make sure you have parsed the data correctly.

var dateFormat = "yyyyMMdd"; // this could be pulled from app.config or some other config source

foreach (var file in filestoextract)
{
    var dateStr = file.Substring(file.Length-dateFormat.Length);
    if (ValidateDate(dateStr, dateFormat))
    {
        var id = file.Substring(0, file.Length - (dateFormat.Length+1));
        // do something with data
    }
    else
    {
        // handle invalid filename
    }
}

public bool ValidateDate(stirng date, string date_format)
{
    try
    {
        DateTime.ParseExact(date, date_format, DateTimeFormatInfo.InvariantInfo);
    }
    catch
    {
        return false;
    }
    return true;    
}


You could use a Regex :

match = Regex.Match ("khwm20110910","(?<code>.*)(?<date>.{6})" );
Console.WriteLine (match.Groups["code"] );
Console.WriteLine (match.Groups["date"] );

To explain the regex pattern (?<code>.*)(?<date>.{6}) the brackets groups creates a group for each pattern. ?<code> names the group so you can reference it easily.

The date group takes the last six characters of the string. . says take any character and {6} says do that six times.

The code group takes all the remaining characters. * says take as many characters as possible.


for each(string part in stringList)

{
    int length = part.Length;
    int start = length - 8;
    string dateString = part.Substring(start, 8);
}

That should solve the variable length to get the date. The rest of the pull is most likely dependent on a pattern (suggested) or the length of string (when x then the call is 4 in length, etc)


If you ID isn't always the same amount of letters you should seperate the ID and the Date using ',' or somthing then you use this:

for( int 1= 0;i< filestoextract.count;1++)
{
    string extractname = filestoextract[i].ToString();
    string ID = extractname.substring(0, extractname.IndexOf(','));
    string Date = extractname.substring(extractname.IndexOf(','));
    Console.WriteLine(ID + Date);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消