Hi sir this is lavanya. I am begginer in vb.net. I have a problem in deleting the first two words in a string. I am not using the loops.
lavanya uppala 0 Newbie Poster
cgeier 187 Junior Poster
If you don't want to use a loop, you could use "Substring" and "IndexOf(" ").
Edited by cgeier
PerplexedB 2 Newbie Poster
Frankly, I don't see how you can do this without a loop, but here is one solution :
Sub Main()
Console.WriteLine(removeFirstTwoWords("word1 word2 word3 word4"))
Console.ReadKey()
End Sub
Function removeFirstTwoWords(s As String) As String
Dim a() As String = Split(s, " ")
Dim result As String = ""
For i As Integer = 2 To a.Length - 1
result &= IIf(i = 2, "", " ") & a(i)
Next
Return result
End Function
Edited by PerplexedB
cgeier 187 Junior Poster
The easiest way to do it without using a loop is to use recursion. I will show you two recursive methods. The difference is the placement of the subtraction from the counter (numberOfWords = numberOfWords - 1
). This determines whether you will use <=
or just <
in your "if" statement.
Note: The counter parameter numberOfWords
must be passed using ByRef
for this to work.
If we decrement after the "if" statement we do something like the following:
Version 1:
Private Function removeWords(ByVal userStr As String, ByRef numberOfWords As String)
Dim retStr As String = String.Empty
'if we decrement after this statement,
'we use <= here
If numberOfWords <= 0 Or userStr.Length = 0 Then
Return userStr
End If
...
'decrement counter
numberOfWords = numberOfWords - 1
'recursively call removeWords
Return removeWords(retStr, numberOfWords)
End Function
removeWords function:
Private Function removeWords(ByVal userStr As String, ByRef numberOfWords As String)
Dim retStr As String = String.Empty
'return the string when it is either
'empty or the desired number of words
'have been removed
If numberOfWords <= 0 Or userStr.Length = 0 Then
Return userStr
End If
'remove any leading or trailing spaces
retStr = userStr.Trim()
If retStr.Contains(" ") Then
'string contains more than 1 word
retStr = retStr.Substring(retStr.IndexOf(" ") + 1)
Else
'string contains only 1 word
retStr = String.Empty
End If
'decrement counter
numberOfWords = numberOfWords - 1
'recursively call removeWords
Return removeWords(retStr, numberOfWords)
End Function
Version 2:
If we decrement before the "if" statement we do something like the following:
Private Function removeWords(ByVal userStr As String, ByRef numberOfWords As String)
Dim retStr As String = String.Empty
'decrement counter
numberOfWords = numberOfWords - 1
'if we decrement before this statement,
'we use <
If numberOfWords < 0 Or userStr.Length = 0 Then
Return userStr
End If
...
'recursively call removeWords
Return removeWords(retStr, numberOfWords)
End Function
removeWords function:
Private Function removeWords(ByVal userStr As String, ByRef numberOfWords As String)
Dim retStr As String = String.Empty
'decrement counter
numberOfWords = numberOfWords - 1
'return the string when it is either
'empty or the desired number of words
'have been removed
If numberOfWords < 0 Or userStr.Length = 0 Then
Return userStr
End If
'remove any leading or trailing spaces
retStr = userStr.Trim()
If retStr.Contains(" ") Then
'string contains more than 1 word
retStr = retStr.Substring(retStr.IndexOf(" ") + 1)
Else
'string contains only 1 word
retStr = String.Empty
End If
'recursively call removeWords
Return removeWords(retStr, numberOfWords)
End Function
Usage:
Dim myStr As String = String.Empty
myStr = removeWords("word1 word2 word3 word4", 2)
Edited by cgeier
cgeier 187 Junior Poster
One more way to do it (without using a loop), is to create a method that removes one word, and call it repeatedly. Although, I advise against this method.
Private Function removeWord(ByVal userStr As String)
Dim retStr As String = userStr
'remove any leading or trailing spaces
retStr = userStr.Trim()
If retStr.Contains(" ") Then
'string contains more than 1 word
retStr = retStr.Substring(retStr.IndexOf(" ") + 1)
Else
'string contains only 1 word
retStr = String.Empty
End If
Return retStr
End Function
Usage:
Dim myStr As String = String.Empty
'call 2 times to remove 2 words
myStr = removeWord(myStr)
myStr = removeWord(myStr)
PerplexedB 2 Newbie Poster
I stand corrected. This is doable without a loop. Cgeier, good thinking. :)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.