Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,912 questions

51,844 answers

573 users

How to get the last two words from a string in VB.NET

2 Answers

0 votes
Imports System

Public Class Test
    Public Shared Sub Main()
        Dim s As String = "c# javascript php c c++ python vb.net"
        Dim array() As String =  s.Split(" ")
 
        Dim last_word As String = array(array.Length - 2) + " " + array(array.Length - 1)
 
        Console.Write(last_word)
    End Sub
End Class



' run:
'
' python vb.net
'

 



answered Sep 9, 2019 by avibootz
0 votes
Imports System

Public Class Test
    Public Shared Sub Main()
        Dim s As String = "c# javascript php c c++ python vb.net"
        
        Dim pos1 As Integer = s.LastIndexOf(" ")
        Dim pos2 As Integer = s.LastIndexOf(" ", pos1 - 1)
   
        Console.WriteLine(pos1) ' debug
        Console.WriteLine(pos2) ' debug

        Dim last_two_words As String = if ((pos2 > -1), s.Substring(pos2 + 1), s)
 
        Console.Write(last_two_words)
    End Sub
End Class



' run:
'
' 30
' 23
' python vb.net
'

 



answered Sep 9, 2019 by avibootz

Related questions

1 answer 113 views
1 answer 191 views
2 answers 167 views
1 answer 107 views
2 answers 154 views
1 answer 163 views
...