Saturday, March 31, 2012

Trim Trailing Comma in an Array

Hi - I thought this would be easy but I can't find an answer. I have an ArrayList seperated by commas and I want to trim off the last comma in the list. The VB function throws an error. Thanks

Dim myList As New ArrayList

'Does not work
myList.Trim()

Hi,

You need to explain your problem a little better.

Do you have an arraylist of strings and you want to trim off the last comma of each string in the arraylist ?

Roy.


I'm not sure if I understand what you are trying to do. How do you have an ArrayList seperated by commas? The point of an Array is that each item is accessible through an index, and it doesn't need a delimeter. If you want to remove an item from the array, you can use myList.Remove(object to remove) or myList.RemoveAt(index to remove). The Trim method is for strings.

Hi - I'm populating an arraylist from a datareader like below

Dim myList As New ArrayList
Dim dr As SqlDataReader
Dim count As Integer

strSQLConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnString"))
strSqlText = "getIndustries"
cmd = New SqlCommand(strSqlText, strSQLConn)
cmd.CommandType = CommandType.StoredProcedure

Try
strSQLConn.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
With dr
If .HasRows Then
While .Read
myList.Add(.GetString(0))
myList.Add(",")
count += 1
End While
End If
End With

End result below. I need to remove that last comma at the end of Video Rentals.

Travel Services,Trucking & Shipping,Utilities Electric,Utilities Natural Gas,Utilities Water & Waste,Video Rentals,


Ok the easy answer is to use:

myList.RemoveAt(myList.Count - 1);

However I have to ask, why are you inserting commas after each value? And why are you using an arraylist? It seems to me that a string array would be more efficient. Also, what's the purpose of the "count" integer? You can use myList.Count to see how many objects are in the ArrayList (assuming you aren't inserting commas in between).


To take off the last comma in the last element of the Array, you could use something like

myList.Item(myList.Count - 1) = myList.Item(myList.Count - 1).ToString.Trim(",")

Depending on what you are doing with the array later, it may work better to leave the commas out of the array and just add them when you display the contents of the Array.

Otherwise, you could just build a string instread of an array.

Something like this:

Dim myList As New StringBuilder()While .Read myList.Append(.GetString(0)) myList.Append(",") count += 1End While

Heck, now that I think of it, you could be using a StringBuilder too, which is *much* more efficient with a large number of concatenations:
Dim myListAs New ArrayListDim drAs SqlDataReaderDim countAs Integer Dim mySBAs New StringBuilder() strSQLConn =New SqlConnection(ConfigurationSettings.AppSettings("ConnString")) strSqlText ="getIndustries" cmd =New SqlCommand(strSqlText, strSQLConn) cmd.CommandType = CommandType.StoredProcedureTry strSQLConn.Open() dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)With drIf dr.HasRowsThen While dr.Read mySB.Append(dr.GetString(0) + ",") count += 1End While End If End With

Then you could do something like this to trim it:

String myResult = mySB.ToString().TrimEnd(",".ToCharArray());
HTH

If your array is a string like

Dim sTest = "A,B,C,D,"

Then a test along the lines of

If sTest.EndsWith(",") Then sText = sTest.Substring(0, sText.Length - 1)

should do it.

(I am awaiting my copy of VS2005 as part of my MSDN subscription, so the above is unchecked as to syntax.)



hello ,

here is a little idea i dont know if this function is available in vb.net since i m a c# programmer

may be it help you ,

mylist.RemoveAt(mylist.LastIndexOf(

","));

Regards

Gurpreet


Thanks very much!

0 comments:

Post a Comment