13 Replies - 1044 Views - Last Post: 06 August 2011 - 09:10 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Retrieve part of the string

Posted 05 August 2011 - 01:21 PM

Hi people.
I have several values in a column like:
323,33 KB
532 Bytes
1,3 MB
So the string is like:
Dim A As String = "323,33 KB"
Dim B As String = "532 Bytes"
Dim C As String = "1,3 MB"

I would like to retrieve only the values (I mean without KB, Bytes..etc).
How can I do that?
Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve part of the string

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,267
  • Joined: 12-June 08

Re: Retrieve part of the string

Posted 05 August 2011 - 01:24 PM

string.replace is a great place to start. Replace a specific substring with an empty string and voila!
Was This Post Helpful? 0
  • +
  • -

#3 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 05 August 2011 - 04:03 PM

Ok, thats fine. I got some results:
Dim values As New List(Of Long)
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Contains("KB") Then
                values.Add(Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Replace(" KB", ""))
            End If
        Next
        Dim totalValue As Long
        For i As Integer = 0 To values.Count - 1
            totalValue += CLng(values.Item(i))
        Next

Then I tried:
If totalValue < 1024  Then
MsgBox(Fix(totalValue) & " Bytes")
ElseIf totalValue >= 1024 Then
            MsgBox(Format(totalValue / 1024, "#0.00") & " KB")
ElseIf totalValue >= 1048576 Then
            MsgBox(Format(totalValue / 1024 / 1024, "#0.00") & " MB")
End If


And, it retrieves me 1,52 KB...and I have fields with 600,00 KB, 323,33 KB etc..so this is not possible.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,267
  • Joined: 12-June 08

Re: Retrieve part of the string

Posted 05 August 2011 - 04:23 PM

So where did the replace go in the second half?
Was This Post Helpful? 0
  • +
  • -

#5 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 05 August 2011 - 04:38 PM

Sorry? (I didn't understand)

This post has been edited by Keylogger: 05 August 2011 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,267
  • Joined: 12-June 08

Re: Retrieve part of the string

Posted 05 August 2011 - 06:23 PM

You said "then I tried"... and "so this is not possible". That code doesn't have the 'replace' method used. Why not?
Was This Post Helpful? 0
  • +
  • -

#7 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 05 August 2011 - 07:08 PM

Doesn't have? Yes it has. See it again.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,267
  • Joined: 12-June 08

Re: Retrieve part of the string

Posted 05 August 2011 - 07:58 PM

View PostKeylogger, on 05 August 2011 - 06:03 PM, said:

Ok, thats fine. I got some results:
Dim values As New List(Of Long)
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Contains("KB") Then
                values.Add(Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Replace(" KB", ""))
            End If
        Next
        Dim totalValue As Long
        For i As Integer = 0 To values.Count - 1
            totalValue += CLng(values.Item(i))
        Next

Then I tried:
If totalValue < 1024  Then
MsgBox(Fix(totalValue) & " Bytes")
ElseIf totalValue >= 1024 Then
            MsgBox(Format(totalValue / 1024, "#0.00") & " KB")
ElseIf totalValue >= 1048576 Then
            MsgBox(Format(totalValue / 1024 / 1024, "#0.00") & " MB")
End If


And, it retrieves me 1,52 KB...and I have fields with 600,00 KB, 323,33 KB etc..so this is not possible.

In that part there - I don't see it.
Was This Post Helpful? 0
  • +
  • -

#9 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 06 August 2011 - 06:02 AM

If you're talking about this piece of code:
If totalValue < 1024  Then
MsgBox(Fix(totalValue) & " Bytes")
ElseIf totalValue >= 1024 Then
            MsgBox(Format(totalValue / 1024, "#0.00") & " KB")
ElseIf totalValue >= 1048576 Then
            MsgBox(Format(totalValue / 1024 / 1024, "#0.00") & " MB")
End If

You don't see the function Replace() because it's not necessary. I've already worked with that function before, in the 1st part of the code, here:
If Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Contains("KB") Then
values.Add(Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Replace(" KB", ""))
End If

So the values added to the List(Of Long) will be without the characters ("KB, Bytes, Etc")
After make this, I must sum all the values that my List contains, so I made:
Dim totalValue As Long
        For i As Integer = 0 To values.Count - 1
            totalValue += CLng(values.Item(i))
        Next


That's my logic working on..but I'm not pretty sure it's ok.
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Retrieve part of the string

Posted 06 August 2011 - 06:27 AM

If you need to do maths on them then you have to parse them to Integer. Example if I have the original string:
 Dim x As String = "32333 KB"
        x = x.Replace(" KB", "")
        Dim x1 As Integer = Integer.Parse(x) 

Also if your string contains commas, you must remove them before parsing. Also no need to check if the string you want to replace exists or not the Replace() function will do it for you
Was This Post Helpful? 0
  • +
  • -

#11 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 06 August 2011 - 06:42 AM

Integer.Parse() function does not help me in nothing, I think. I tested and it's the same without the function.
Let's go for parts:
1. Get all the values from one column in DataGridView and then throw it to a List():
Dim values As New List(Of Long)
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            values.Add(Me.DataGridView1.Rows(i).Cells(6).Value.ToString.Replace(" KB", ""))
        Next  

2. Sum all the values inserted before:
       Dim totalValue As Integer
        For i As Integer = 0 To values.Count - 1
            totalValue += Integer.Parse(values.Item(i))
        Next

3. Format the final value in MB, KB or Bytes:
If  totalValue >= 1048576 Then
            MsgBox(Format(totalValue / 1024 / 1024, "#0.00") & " MB")
        ElseIf totalValue >= 1024 Then
            MsgBox(Format(totalValue / 1024, "#0.00") & " KB")
        ElseIf totalValue < 1024 Then
            MsgBox(Fix(totalValue) & " Bytes")
        End If

The problem is that I've added to the List() values like: 633KB, 550KB, 993KB, which makes more than 1MB, and it retrieves me 1,34 KB (or something like that).
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Retrieve part of the string

Posted 06 August 2011 - 06:59 AM

The problem is that you pass them as KB and then in formatting, you treat them as bytes. If you need to treat them as bytes, then change them into bytes while adding:
 Dim totalValue As Integer
 For i As Integer = 0 To values.Count - 1
     totalValue += 1024 * Integer.Parse(values.Item(i))
 Next 

Was This Post Helpful? 1
  • +
  • -

#13 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Retrieve part of the string

Posted 06 August 2011 - 08:41 AM

Totaly correct. It works with your solution.
Thanks!
Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Retrieve part of the string

Posted 06 August 2011 - 09:10 AM

Glad we could help :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1