-8

Hey all I need some help with finding how to code the following in order to count the number of . (periods) in a string.

Say the string was 102.12.97.258 and I wanted to know how many periods there were in that string. The answer, obvious, would be 4.

All I can find would be the instr() function but that seems to only find if it finds only 1 instance of that period within the string. I'm looking to get how many periods are within the string.

Any help would be great!

not a dup There are better ways of doing this via the all ready posted answers here. If they were exact matches of the code on the dup page then yeah... but its not.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • 9
    uhhh... there are 3 periods in an IP address. Not 4. – Corey Ogburn Sep 01 '15 at 15:12
  • 1
    `102.12.97.258` I count 3; if you were to use `String.Split()` using a period, you'd get 4 segments which sounds like what you are trying to determine – Ňɏssa Pøngjǣrdenlarp Sep 01 '15 at 15:12
  • 3
    You _may_ be looking for [`IPAddress.TryParse`](https://msdn.microsoft.com/en-us/library/system.net.ipaddress.tryparse(v=vs.110).aspx)? – James Thorpe Sep 01 '15 at 15:13
  • @CoreyOgburn Wow... I must be half asleep to have missed that.... Thanks for pointing that out! – StealthRT Sep 01 '15 at 15:15
  • @JamesThorpe make that an answer so I can give you proper credit. – StealthRT Sep 01 '15 at 15:23
  • Just upvote the dupe link - there is already an answer here – Ňɏssa Pøngjǣrdenlarp Sep 01 '15 at 15:25
  • Note that it's arguably not a dupe of that Q as it's c#, not vb - I've seen a meta post where the consensus was not to mark such things as dupes. Typically, can't find it now... – James Thorpe Sep 01 '15 at 15:26
  • I don't see any reference to IP addresses in the question, but well... Perhaps the title (an IP address has 4 dots - 1) :) – varocarbas Sep 01 '15 at 15:26
  • Not a dup. Please stop down voting. – StealthRT Sep 01 '15 at 15:27
  • 1
    Here's a VB version: [Count specific character occurrences in string](http://stackoverflow.com/questions/5193893/count-specific-character-occurrences-in-string) – James Thorpe Sep 01 '15 at 15:28
  • Regarding the downvotes - as written the question is about counting strings. As has been shown, there's other questions about that. So reason 1 for a downvote: lack of research. The edit shows you're talking about IP Addresses - that wasn't in the initial question. This (coupled with _"The answer, obvious, would be 4."_ when it's obviously 3) makes the question "unclear", reason 2 for a potential downvote. – James Thorpe Sep 01 '15 at 15:33
  • 1
    C# answer `source.Split('/').Length - 1; `VB, localized answer `source.Split('.'c).Length - 1` not a great deal of difference, they are both using a common ordinary NET method – Ňɏssa Pøngjǣrdenlarp Sep 01 '15 at 15:44
  • 4
    You asked to find the number of dots in a string, which is [a](http://stackoverflow.com/questions/5193893/count-specific-character-occurrences-in-string) [dup](http://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-within-a-string). But your real question should've been how to detect if it's a valid ip address, which is also a [dup](http://stackoverflow.com/questions/799060/how-to-determine-if-a-string-is-a-valid-ipv4-or-ipv6-address-in-c). This was a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – the_lotus Sep 01 '15 at 15:53

2 Answers2

0

An easy way to count the number of occurrence of a certain character is to do a replace with nothing and subtract the length from the original

Dim orig As String
Dim NumberOfChar As Integer

orig = "102.12.97.258"

NumberOfChar = orig.Length - orig.Replace(".", "").Length
jradich1234
  • 1,410
  • 5
  • 24
  • 29
0

How about Linq Count:

Dim s As String = "102.12.97.258"
Dim count = s.Count(Function(x) x = "."c)

But it sounds as though you need to check if it is an IP Address? so this would be more appropriate:

If IPAddress.TryParse(s, Nothing) Then
     'it is an ip address
End If
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143