views:

93

answers:

8

Now it seems like a really simple question and I may just be being thick, but what I'm trying to achieve is basically print an array to screen in the following format:

Item 1, Item 2, Item 3, Item 4

Although I say to screen, as that was the best way I could describe it, I'm actually writing it to the page inside some Javascript.

The way I'm currently going about writing it out is as follows:

for each b in theDates
    Response.Write("'" + b.CallDate + "',")
next

But obviously that returns a string of Item 1, Item 2, Item 3, Item 4,.

Is there a simple way of getting rid of the last comma or am I going about this completely wrong?

+5  A: 

Try String.Join (documentation). My VB is rusty, but hopefully you can read this C#:

var datesInQuotes = theDates.Select(date => "'" + date.CallDate + "'");

Response.Write(String.Join(", ", datesInQuotes));
Douglas
I'm surprised the OP chose an answer that does not address his question. It leaves the trailing comma AND a space AND an inverted comma! This is the code that does what he/she requested.
Alex Essilfie
Alex Essilfie
@Alex: Cheers, fixed that up. The chosen answer only sets the st variable in the loop, the Response.Write always ends with "'". A good reason not to use that code, it's easy to miss-read what it does!
Douglas
@Alex: I don't end up with the issue you mentioned at all with the accepted answer. I have infact tried all of the answers received in this question and received the same result as that.I will be considering the string.join method as the final method in the application based on one of Douglas' comments, but right now, it does what I want.
Liam
+6  A: 

You could use String.Join method. Not sure about VB but something like that:

Response.Write(String.Join(", ", theDates.Select(Function(n) "'" & n.CallDate & "'").ToArray()))
Pavel Morshenyuk
+3  A: 

As others have said, String.Join is what you want in this case.

More generically: you've got to either detect when you're at the the last element and not append a comma, or detect when you're at the first element and not prepend a comma.

Typically it's easier to detect when you're at the first, so:

dim firstDone as bool = false
for each b in theDates
    if firstDone then Response.Write (",")
    Response.Write("'" + b.CallDate + "'")
    firstDone = true
next

(Excuse my rusty vb)

Benjol
Your code leaves a trailing comma. I'm sure the OP specifically said he wanted to get `'rid of the last comma'`
Alex Essilfie
@Alex I don't think it does leave a trailing comma. Every time it writes a comma, it writes a date afterwards.
MarkJ
@Alex, I've renamed my variable, does that make it easier to understand?
Benjol
A: 

No, you aren't wrong - totally.

Loop through theDates and put the data into a string.

After you are done looping, remove the last comma:

myString.Remove(myString.Length - 1, 1);

Response.Write(myString)

A side note: You really shouldn't output to the screen with Response.Write. You should set the text of a server control as your output. A label is perfectly fine. You will notice that your text won't appear where you think it will if you don't.

TheGeekYouNeed
This'll build the string twice in memory, it'd be better to build it correctly from the start.
sarnold
A: 

You could write the comma in front of each item except the first one.

Kaniu
+2  A: 

There's no need to String.join this.

st="'"
for each b in theDates
    Response.Write(st + b.CallDate + "'")
    st=", '"
next
Luther Blissett
Obviously this works, but I think that using String.Join to join strings expresses the intent more clearly, so it should still be considered.
Douglas
Agreed. I'm considering changing the code to string.join anyway for the final version, though currently this does the job. In fact all the answers I've been given do pretty much.
Liam
You C# people are strange folks.
Luther Blissett
String.Join is obvious enough. The lambda functions do look a bit odd until you get used to them.
MarkJ
A: 

I would recommend to do just as you were doing, and then use the Response.Remove(s.Length-1, 1)

Jcei
A: 

In C#:

for (int i = 0; i < theDates.Length; i++)
{
    if (i > 0)
    {
        Response.Write(",");
    }
    Response.Write(theDates[i]);
}