-2

For Example : String = Visual BasiC output = V C

I have tried searching everywhere but found none, is it possible for vb.net to do this one?

  • 2
    [Research Regex (Regular Expressions)](https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx). As this should be able to do it – Cal-cium Nov 07 '17 at 14:47
  • The [Char.IsUpper Method](https://msdn.microsoft.com/en-us/library/system.char.isupper(v=vs.110).aspx) is a simple test to perform. Did you intend to include the space in the output? – Andrew Morton Nov 07 '17 at 15:17
  • Please read [ask] and take the [tour]. This is not a tutorial site and we are not here to do your homework for you – Ňɏssa Pøngjǣrdenlarp Nov 07 '17 at 15:39

1 Answers1

2

The following code iterates forward through the string until it finds a capital letter, adds that to the result and exits the first loop. It then iterates backwards through the string until it finds a capital letter, adds it to the result and exits the loop. Finally, it returns the result to the calling code.

I suspect that this is a school/college assignment, so I would suggest that you read Open letter to Students with homework problems

Private Function FirstAndLastCapitalLetter(s As String) As String
    Dim result As String = ""
    For i As Integer = 0 To s.Length - 1
        If s.Substring(i, 1) = s.Substring(i, 1).ToUpper Then
            result = result & s.Substring(i, 1)
            Exit For
        End If
    Next
    For i As Integer = s.Length - 1 To 0 Step -1
        If s.Substring(i, 1) = s.Substring(i, 1).ToUpper Then
            result = result & s.Substring(i, 1)
            Exit For
        End If
    Next
    Return result
End Function
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • 1
    Using `.ToUpper()` does not always do what you expect. Jon Skeet gives a really good talk which includes the problem: [Back to basics: the mess we'vw made of our fundamental data types](https://www.youtube.com/watch?time_continue=1264&v=l3nPJ-yK-LU). That link starts some way in - I recommend watching the whole thing :) – Andrew Morton Nov 08 '17 at 09:24
  • Now I understand why NodaTime took so long! – David Wilson Nov 08 '17 at 11:38
  • 1
    While I'm sure this works, you're creating a lot of strings! Your `If` statement alone creates 3 separate strings. You can access a character in a string using `s(i)` and use the `Char.IsUpper` method to determine if it upper case. – Chris Dunaway Nov 08 '17 at 14:48