Visual Basic | Replace Substring in String with String [closed] - vb.net

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using Visual Basic, I want to replace a text within a String with another String. I do not know the location of this text within the String. How can I accomplish this?
Thanks for any help.

Try:
X = X.Replace("original"," toReplace")
Or this link:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Input string.
Dim input As String = "Dot Net Not Perls"
' Use Regex.Replace with string arguments.
Dim output As String = Regex.Replace(input, "N.t", "NET")
' Print.
Console.WriteLine(input)
Console.WriteLine(output)
End Sub
End Module
Output
Dot Net Not Perls
Dot NET NET Perls

The replace method does not require you to know the location. You specify the original value, the string to look for, and the string to change it too. Very straight forward.
Dim aString As String = Replace("String to Search", "String to Find", "String to Replace With")

Related

Visual Basic: Reading Filenames from a Directory [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to write a simple VB program to read the file names in a given directory. This is what I've written:
Imports System
Imports System.IO
Imports System.Text
Module Program
Sub Main(args As String())
Dim FolderPath As String = 'C:\Users\uarenet\Downloads\Projects\Mock_Tables\_ExcelTables'
Dim FileNames As String() = Directory.GetFiles(FolderPath)
For Each FileName As String In FileNames
Console.WriteLine(FileName)
Next
End Sub
End Module
For some reason Visual Studio is not recognizing the line Dim FileNames.... statement, and, because of this, refuses to process the For Each.... statement.
Thanks in advance for any help or suggestions you can offer.
You used this character ' to create a string although this character in Visual Basic language used for writing comments not string as you expected, string written in double quotes like "Hello world", so I suggest replacing your code with this:-
Sub Main(args As String())
Dim FolderPath = "C:\Users\uarenet\Downloads\Projects\Mock_Tables\_ExcelTables"
Dim FileNames = Directory.GetFiles(FolderPath)
For Each FileName As String In FileNames
Console.WriteLine(FileName)
Next
End Sub

How can I delimit with characters using vba? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Example: consider there are 5 names separated with a semicolon. I want to separate these 5 names.
consider,
Richard;Pareek;Elizabeth;Creig;Tomy
You would use the Split() function.
Example:
Dim input as String
Dim results() as String
input = "Richard;Pareek;Elizabeth;Creig;Tomy"
results = Split(input,";")
Note: If there are are spaces after the semicolons, be sure to include that in the second parameter of the Split function (so "; " instead of ";")

VBA - Insert string if matching criteria is found anywhere in textbox [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write a macro that inserts "!!!" at the beginning of a textbox if there is a "XX" located in any part of that text box. Ideally the macro will run this procedure for every textbox in the presentation, but I can figure out how to loop it through if someone can help me with the basic procedure.
For example, a text box with the following text:
I ate XX hamburgers on XX/XX/20XX
would become
!!!I ate XX hamburgers on XX/XX/20XX
I hope this can help.
Sub test()
Dim TestString As String
TestString = "I ate XX hamburgers on XX/XX/20XX"
Variable = InStr(1, TestString, "X")
If Variable > 0 Then
output = "!! " & TestString
End If
Debug.Print output
End Sub
Here TestString = your input string
The InStr function tests if "X" is present in the string, if it is then "!!" is joined to the variable "Output"
This should be quite easy to adapt?
i hope this will help you.
Sub test()
Dim s As String
s = "Test XX"
If InStr(1, s, XX, vbTextCompare) Then
s = "!!!" + s
End If
MsgBox s
End Sub

Fetching last value from a comma separated string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am new to VB.
I am trying to retrieve the last value in the csv below.
0066,0055,0078
From this string, I want to fetch 0078.
From this string, I want to fetch 0078.
Try this,
Dim xString as String = "0066,0055,0078"
Dim xResult() as String = xString.split(",")
Dim xExtracted as String = xResult(xResult.length-1)
MsgBox(xExtracted)
a shorter alternative would be:
Dim xString as String = "0066,0055,0078"
MsgBox(xString.Substring(xString.LastIndexOf(",") + 1))
if you are going to use the function regularly consider creating an extension method

Deduplicate Text file VB.NET [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a large text file with over 100k of lines. Some of the lines are duplicates. I would like to be a to dedupe these entries before processing them. I am using visual basic 2010 Express to write this.
Text file example:
132165
165461
646843
654654
321358
132165
165461
I want to dedupe these entries before processing them
You can use a HashSet(Of T)
Dim nodupes As New HashSet(Of String)(File.ReadLines(path))
For Each str As String In nodupes
' no duplicate here '
Next
Edit Since a HashSet(Of T) does not guarantee to preserve the insertion order you can use following code if you need to ensure this order:
Dim nodupeSet As New HashSet(Of String)
Dim nodupes = From line In File.ReadLines(path)
Where nodupeSet.Add(line)
For Each str As String In nodupes
' no duplicate here '
Next