1

I have text in Excel like this:

            120
124569       abasd          12345

There are sapces both to the left and to the right side.

I copy this from Excel and paste as text. When I check this, it shows like this when I click on button.

Code:

abArray= abArray & "," & gridview1.Rows(i).Cells(2).Text

For k = 3 To 17
    bArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text)
Next

In abArray this shows as:

0, abasd      ,12345,0,0,0,0,0

I want to remove/trim spaces both from left and right.

I have tried abArray.Trim() but this still show spaces.

Bugs
  • 4,491
  • 9
  • 32
  • 41
user 123
  • 41
  • 2
  • 13

3 Answers3

0

Use the VB.NET Trim function to remove leading and trailing spaces, change this one line of code:

abArray= abArray& "," & Val(Trim(gridview1.Rows(i).Cells(k).Text))

abArray.Trim() does not work because you did not give the Trim function anything to trim.

Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34
  • this shows error .. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. ... – user 123 Apr 26 '17 at 11:23
  • That's probably because your loop either begins or ends with k being a invalid index into Cells. Put a breakpoint on the line of code `abArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text.Trim)` and see how many cells there are and what the value of k is. – Prescott Chartier Apr 26 '17 at 11:49
  • ok what if we remove characters like i have number like this .. 101730921  so there is special characters so i remove these? – user 123 Apr 27 '17 at 08:05
  • I would use RegEX, there is a good example of that here: http://stackoverflow.com/questions/3701018/remove-special-characters-from-a-string – Prescott Chartier Apr 27 '17 at 11:16
0

If you want to remove all the spaces out of the end result consider String.Replace:

Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

Example use:

Dim s As String = "0, abasd      ,12345,0,0,0,0,0"
s = s.Replace(" ", "")

This would output:

0,abasd,12345,0,0,0,0,0

It may also be worth using a StringBuilder to join all your values together as this is good practice when looping as you are. At this point you could use String.Trim. This would preserve any spaces that are within your value. In order words it would only remove the spaces from the beginning and the end of the value.

Example use:

Dim sb As New StringBuilder
For k = 0 To 17
    sb.Append(String.Format("{0},", gridview1.Rows(i).Cells(k).Text.Trim()))
Next

Dim endResult As String = sb.ToString().TrimEnd(","c)

endResult would output:

0,abasd,12345,0,0,0,0,0

You will have to import System.Text in order to make use of the StringBuilder class.

Bugs
  • 4,491
  • 9
  • 32
  • 41
0

Try it like this

    abArray = abArray & "," & gridview1.Rows(i).Cells(2).Text.Trim

    For k = 3 To 17
        abArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text.Trim)
    Next
dbasnett
  • 11,334
  • 2
  • 25
  • 33