0
Dim strOrig = "192/8' 33/5' 76/24' 17/12'"

Hi all, I want to obtain every number before the "/". The output will be 318. Regarding how to achieve it, what I can think of is:

1.Split all the segment by looking for 'spacing' as end point for each segment and put them into an array. E.g. (0)192/8, (1)33/5, (2)76/24 etc...

2.By looping the array, look for the slash "/" and get the number before it and sum it until loop end. E.g. (0)192, (1)33, (2)76 etc...

I would like to know if my method was worth the effort as I would like to learn any MORE effective ways than this. Thanks all.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
4 Leave Cover
  • 1,248
  • 12
  • 40
  • 83

3 Answers3

7

You can use LINQ:

Dim strOrig = "192/8' 33/5' 76/24' 17/12'"
Dim numbers = From word In strOrig.Split()
              Let number = word.Split("/"c).First().Trim().TryGetInt32()
              Where number.HasValue
              Select number.Value

Dim sum As Int32 = numbers.Sum()  ' 318

I've used following extension to try-parse a string to Integer?:

<Extension()>
Public Function TryGetInt32(Str As String) As Nullable(Of Int32)
    If Str Is Nothing Then Return Nothing
    Dim num As Int32
    If Int32.TryParse(Str, num) Then Return num
    Return Nothing
End Function
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • (don't forget the VB.NET line-continuation underscore in that LINQ) – valverij Apr 28 '15 at 13:33
  • 1
    @valverij: not needed with VS 2010 – Tim Schmelter Apr 28 '15 at 13:34
  • Any particular reason to choose `Int32` over `Integer` ? – asawyer Apr 28 '15 at 13:34
  • Really? Is that a VS 2010 thing or a .NET 4.5 thing? I definitely remember needing it in 2010 – valverij Apr 28 '15 at 13:34
  • 1
    @valverij: i'm using VS 2010 since 2011 and no, you don't need it anymore. I prefer Int32 over Integer because it is shorter and Int32 is also clearer since there are also Int16 and Int64 and the UInt variants. – Tim Schmelter Apr 28 '15 at 13:36
  • @4LeaveCover: i think that you then really need the line continuation characters. Try to append `_`(empty space and underscore) at the end of each line in the query. Here is an example: https://code.msdn.microsoft.com/windowsapps/Implicit-Line-Continuation-a044cbe0 – Tim Schmelter Apr 28 '15 at 13:40
  • After I apply _ behind the extension, it throws me error Type 'Extension' is not defined. – 4 Leave Cover Apr 28 '15 at 13:45
  • @4LeaveCover: the extension method `TryGetInt32` must be in a module like following: `Module StringExtensions Public Function TryGetInt32(Str As String) As Nullable(Of Int32) If Str Is Nothing Then Return Nothing Dim num As Int32 If Int32.TryParse(Str, num) Then Return num Return Nothing End Function End Module`. But remember to add `Imports System.Runtime.CompilerServices` at the top. – Tim Schmelter Apr 28 '15 at 13:46
1

Regex to the rescue:

    Dim strOrig As String = "192/8' 33/5' 76/24' 17/12'"

    Dim sum as Integer = (From m As Match In Regex.Matches(strOrig, "(?<number>\d+?)/")
                          Select Convert.ToInt32(m.Groups("number").Value)
                         ).Sum()
Community
  • 1
  • 1
0

The most simple I can think of.

    Dim lst As List(Of String) = strOrig.Split("/").ToList

    lst.RemoveAt(lst.Count - 1)
    lst.Sum(Function(x) Convert.ToUInt16(x))
Fjodr
  • 919
  • 13
  • 32