Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
Private Function ReplaceNth(CompleteString As String, occurence As Integer, Find As String, replace As String) As String
    Dim CheckOccurence As Integer = 1
    Dim NewString As String = CompleteString
    While NewString.IndexOf(Find) <> -1
        If occurence < CheckOccurence Then
            Dim temp As String = CompleteString.Substring(0, CompleteString.Length - NewString.Length)
            Return temp.Replace(Find, replace) & NewString
        Else
            Dim a As Integer = NewString.IndexOf(Find) + Find.Length
            NewString = NewString.Substring(a)
            CheckOccurence += 1
        End If
    End While
    Return "Not Found"
End Function  



VB
Dim newProjectFile as new StringBuilder
newProjectFile.append(ReplaceNth(my.computer.FileSystem.ReadAllText(myFile).Replace("COUGAR", "9294653879"), 3,"9294653879","PANTHER")  


Then I used My.Computer.FileSyetem.WriteAllText to rewrite modified file

The result I got is:
the 1st,2nd & 3rd instances of COUGAR were changed to 9294653879 and the 4th instance was changed to PANTHER. The desired result is for instances 1st, 2nd and 4th to be changed to COUGAR and the 3rd to be PANTHER. Additionally, since COUGAR was 6 characters long, I need to delete 4 blank spaces after the 2nd instance where COUGAR became 9294653879.

This is my resulting text file:
VB
CAT**080*          *AZ*1306876735     *ZZ*9294653879          *1*T*:~
GES*PU*1306876735*9294653879*20111008*1611*116712*X*005010X222A1~
NAU1*41*2*RAGGAMUFFIN POOTANG*****46*1306876735~
NENA*440*2*9294653879*****49*PANTHER~



Thanks for any help!
Posted
Updated 12-Jun-12 14:11pm
v5
Comments
RDBurmon 13-Jun-12 9:31am    
Thanks Everyone who replied to this thread , So IvanIT, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

You could use this function:

VB
Private Function ReplaceNth(CompleteString As String, occurence As Integer, Find As String, replace As String) As String
	Dim CheckOccurence As Integer = 1
	Dim NewString As String = CompleteString
	While NewString.IndexOf(Find) <> -1
		If occurence < CheckOccurence Then
			Dim temp As String = CompleteString.Substring(0, CompleteString.Length - NewString.Length)
			Return temp.Replace(Find, replace) & NewString
		Else
			Dim a As Integer = NewString.IndexOf(Find) + Find.Length
			NewString = NewString.Substring(a)
			CheckOccurence += 1
		End If
	End While
	Return "Not Found"
End Function


To use you would:
VB
dim newProjectFile as new StringBuilder
newProjectFile.appext(ReplaceNth(my.computer.filesystem.readalltext(myFile).Replace("COUGAR", "PANTHER"), 3, "PANTHER", "9294653")


Basically what you do is convert all "COUGAR" words to "Panther" (as this is the majority), then use the function to replace only the 3rd instance of "PANTHER" to "9294653"

What would be really cool would be to create an extension method for a string and incorporate the ReplaceNth functionality. Let me know if you need help with that. I did an article on this many moons ago (well on extension methods).

Extension Methods in VB.NET[^]
 
Share this answer
 
Comments
IvanIT 10-Jun-12 21:47pm    
db7uk .... thanks a bunch; this is awesome and works well for me. I am curious about the extension method and would like your pointers on this. Appreciated.
db7uk 12-Jun-12 4:12am    
Sorry, was this answer not correct?. I am still working on the extension method for you but had been caught up on somthing else!
Sandeep Mewara 11-Jun-12 2:32am    
My 5!
The Regex.Replace method with MatchEvaluator parameter explained here http://msdn.microsoft.com/en-us/library/ms149475.aspx[^] can be used for this purpose as shown below:

VB
Dim text As String = "This is sample COUGAR text containing COUGAR" & _
        " word COUGAR five times COUGAR which is to be COUGAR replaced "
Dim ind As Integer = 0
Dim modText As String = Regex.Replace(text,
    "\bCOUGAR\b", Function(m as Match)
        Ind += 1
        Return IF(ind = 3, "9294653", "PANTHER")
    End Function, RegexOptions.CultureInvariant)
Console.WriteLine(modText)
'output
'This is sample PANTHER text containing PANTHER
' word 9294653 five times PANTHER which is to be PANTHER replaced

The input text can be read from file using System.IO.File.ReadAllText and the output can be written to file using System.IO.File.WriteAllText as explained here http://msdn.microsoft.com/en-us/library/ms143368.aspx[^]
 
Share this answer
 
Comments
Espen Harlinn 12-Jun-12 8:40am    
5'ed!
VJ Reddy 12-Jun-12 9:00am    
Thank you, Espen :)
dk7
I am thinking that something is slightly off ... I implemented the code as:

VB
Private Function ReplaceNth(CompleteString As String, occurence As Integer, Find As String, replace As String) As String
    Dim CheckOccurence As Integer = 1
    Dim NewString As String = CompleteString
    While NewString.IndexOf(Find) <> -1
        If occurence < CheckOccurence Then
            Dim temp As String = CompleteString.Substring(0, CompleteString.Length - NewString.Length)
            Return temp.Replace(Find, replace) & NewString
        Else
            Dim a As Integer = NewString.IndexOf(Find) + Find.Length
            NewString = NewString.Substring(a)
            CheckOccurence += 1
        End If
    End While
    Return "Not Found"
End Function


VB
Dim newProjectFile as new StringBuilder
newProjectFile.append(ReplaceNth(my.computer.FileSystem.ReadAllText(myFile).Replace("COUGAR", "9294653879"), 3,"9294653879","PANTHER")


Then I used My.Computer.FileSyetem.WriteAllText to rewrite modified file

The result I got is:
the 1st,2nd & 3rd instances of COUGAR were changed to 9294653879 and the 4th instance was changed to PANTHER. The desired result is for instances 1st, 2nd and 4th to be changed to COUGAR and the 3rd to be PANTHER. Additionally, since COUGAR was 6 characters long, I need to delete 4 blank spaces after the 2nd instance where COUGAR became 9294653879.

Thanks for any help!
 
Share this answer
 
Comments
db7uk 12-Jun-12 19:06pm    
Hiya, sorry is this a question? If so I would be happy to help. Could you re-post by selecting "Improve Question" with an extract of the actual data. I have the code I suggested and would be happy to assist. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900