简体   繁体   中英

How to use indexof and substring to extract specific text from a string and then how to format the extracted string to date time?

example of files[1] string:

C:/Users/test/AppData/LocalLow/DefaultCompany/HorizontalScrollViewDemo/Saved Screenshots\\SavedGameSlot_1920x1080_2022-07-30_22-15-58\\SavedGameSlot_1920x1080_2022-07-30_22-15-58.png

and then i want to extract the part:

2022-07-30_22-15-58

And then to format that to: 7/31/2022 22:15 PM or if it's AM.

then to convert it to string and to assign it to the text.

The problem is that index1 is all the -1 i tried.png or only png and how to format it then to date time and to string?

string[] files = Directory.GetFiles(Directory.GetDirectories(savedGamesFolder)[i]);
int index = files[1].IndexOf("SavedGameSlot_");
int index1 = files[1].IndexOf("png");
string result = files[1].Substring(index, index1);
levelBtnObj.transform.GetChild(0).GetComponent<Text>().text = 

In the end i want to format the result string to date time and then to assign the date time as string to the text in the last line.

Use Regex:

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;


namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "C:/Users/test/AppData/LocalLow/DefaultCompany/HorizontalScrollViewDemo/Saved Screenshots\\SavedGameSlot_1920x1080_2022-07-30_22-15-58\\SavedGameSlot_1920x1080_2022-07-30_22-15-58.png";
            string pattern = @"\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}";
            Match match = Regex.Match(input, pattern);
            DateTime date = DateTime.ParseExact(match.Value,"yyyy-MM-dd_HH-mm-ss", System.Globalization.CultureInfo.InvariantCulture);

        }

    }
 
}

You should use Path.GetFileNameWithoutExtension and String.Substring to get the date and this date-pattern "yyyy-MM-dd_HH-mm-ss" for DateTime.TryParseExact . Then you can use DateTime.ToString to get the desired target format:

string file = "C:/Users/test/AppData/LocalLow/DefaultCompany/HorizontalScrollViewDemo/Saved Screenshots\\SavedGameSlot_1920x1080_2022-07-30_22-15-58\\SavedGameSlot_1920x1080_2022-07-30_22-15-58.png";
string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
string pattern = "yyyy-MM-dd_HH-mm-ss";
// get the end of the file-name with pattern-length
string date = fileName.Length > pattern.Length ? fileName.Substring(fileName.Length - pattern.Length) : null;
if(DateTime.TryParseExact(date, pattern, null, System.Globalization.DateTimeStyles.None, out var dt))
{
    string result = dt.ToString("M/dd/yyyy HH:mm tt", System.Globalization.CultureInfo.InvariantCulture);
}

Read also: custom date and time format string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM