0

I have an array: var array = ["19991229", "19801001", "19890123"]

Then, I want to show this array in a webbrowser form c# by using string.Join("<br>", array)

How can I get the first 4 digit using substring? I want it print:

1999
1980
1989

I can't use string.Join("<br>", array).substring(0,4)

2 Answers 2

4

You can do it with Linq:

var array = new [] { "19991229", "19801001", "19890123" };

var joinedString = string.Join(
    "<br>",
    array.Select(s => s.Substring(0, 4)));

Bonus Edit:

It looks like those strings are actually dates, so here's some extra code to convert them all to DateTime objects:

var dates = array.Select(s => new DateTime(
        int.Parse(s.Substring(0, 4)),
        int.Parse(s.Substring(4, 2)),
        int.Parse(s.Substring(6, 2)))
    );
Sign up to request clarification or add additional context in comments.

Comments

0
 var stArray = new string[] { "19991229", "19801001", "19890123" };
 string stringValue = String.Join("<br>", stArray.Select(x => x.Substring(0,4)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.