VB.NET REPLACE function - vb.net

I am using replace function to replace a character in the file
sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)))
This code is working fine, but now I want to replace two times and I want to use the replace function twice. Something like this, but it is not working . Can anyone tell me how to use Replace function multiple times?
sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)).Replace(strLine, Chr(13), ""))

Your second Replace is using the String.Replace extension, not the Regex.Replace method.
The Regex.Replace function returns a string, not a regex, which is why your second regex call isn't working. For multiple Regex.Replace calls, you would have to do each one individually or modify your replacement statement.
You could probably just use the String.Replace function for this:
sw.WriteLine(strLine.Replace("\\", Chr(13)).Replace(Chr(13), ""))

sw.WriteLine(Regex.Replace(Regex.Replace(strLine, "\\", Chr(13)), Chr(13), "")
Here it is more laid out, so you can see what's going on:
Dim firstIteration = Regex.Replace(strLine, "\\", Chr(13))
Dim secondIteration = Regex.Replace(firstIteration, Chr(13), "")
sw.WriteLine(secondIteration)

Replace a carriage return in a string may be the following ways:
str_souce = str_source.Replace(vbCrLf, "")
str_souce = str_source.Replace(chr(13) & chr(10), "")
str_souce = str_source.Replace(environment.newline, "")
if none above works, try the following one. It can even works for a third party software
str_souce = str_source.Replace(vbCr, "").Replace(vbLf, " ")

Related

VB.net/C# Datatable.Select with special characters (brackets)

I have a vb.net Project with a Dataset with some Cells with Strings like "JB-Y[ST]Y". My Problem is you can't use datatable.select("Column like 'JB-Y[ST]Y'") because of the brackets (the select thinks this is a pattern, but I need an exact match). Anyone has an idea on this topic? I can't find a solution... Thank you!
I can't find a solution using Regex.Replace so I have a brute force solution like this
Public Function ReplaceBrackets(search as String) As String
Dim sb As StringBuilder = New StringBuilder()
For Each c In search
If c = "[" Then
sb.Append("[[]")
Else if c = "]" Then
sb.Append("[]]")
Else
sb.Append(c)
End If
Next
return sb.ToString()
End Function
And you can call it in your Select statement like this
datatable.Select("Column like '" & ReplaceBrackets(search) & "'")

VB.NET - Replace string using or statement

I am having trouble, here is my code.
TextBox2.Text = TextBox1.Text.Replace("a" Or "A", "test")
Simply what I have failed to find a straight answer for. I want to find lowercase "a" and replace it with "test". If it finds a capital "A" I also want to replace it with "test". When I try and OR statement it throws an error. I am looking for a solution, thank you for your time.
You just can't apply and Or operator between two strings.
If you don't want to learn Regex you can concatenate infinite .replace() calls as the return value is a new string.
If you don't care of upper or lower and just want to replace the a character (eather upper and lower) you can use .toUpper() or .toLower() on the input string before passing it to .replace()
Test 1:
Dim input = "abcABC"
output = input.replace("a", "-").replace("A", "-")
Test 2:
Dim input = "abcABC"
output = input.toLower().replace("a", "-")
Test 1 will output this string: "-bc-BC"
Test 2 will output this string: "-bc-bc"
you can simply use a Regex like :
Dim rgx As New RegularExpressions.Regex("[aA]")
If rgx.IsMatch(TextBox1.Text) Then
TextBox2.Text = "test"
End If
OR use this if statement
If TextBox1.text.Contains("a") Or TextBox1.text.Contains("A") Then
TextBox2 = "test"
End If
Or just go to the official documentation of [string.replace] site
as TnTinMn suggested , same case can be found there
TextBox2.Text = TextBox1.Text.Replace("a", "test").Replace("A","test)

remove from String in VB

I have inserted a option in Dorpdown as follows
<option>إختر </option>
When I select this text from server side on any event I get this value
"إختر       ‎"
Now I want to replace this white space in the string. I have tried replace method of String class. But its not working.
str = str.replace(" ","")
Plz suggest
What you should do first is decode the HTML, such that text like but also & are converted to their textual counterparts (" " and "&"). You can do this with: WebUtility.HtmlDecode. Next you can use String.Trim to remove leading and tailing spaces.
Example:
string s = "إختر ";
string r = WebUtility.HtmlDecode(s).Trim();
Or the VB.NET equivalent:
Dim s As String = "إختر "
Dim r As String = WebUtility.HtmlDecode(s).Trim()
Evidently you can try to convert to spaces yourself. But there are examples where it is not that evident and your transcoder can get confused or decode strings the wrong way. Furthermore if in the future the people at W3C change their minds about how to encode text in HTML/XML, then your program will still work.
String.Trim will remove all kinds of white-space including spaces, new lines, tabs, carriage returns, etc. If you only want to remove spaces, you can use: .Trim(' '). Then you specify only to remove the given list of characters (here only ' ').
If you want to remove leading or trailing white-spaces from a string you just need to use String.Trim, but you have to re-assign the return value to the variable since strings are immutable:
string text = "إختر       ‎";
text = text.Trim();
Note that you can also use TrimEnd in this case.
If you want to remove only space characters(not also tabs or new-line characters which are also white-spaces) use:
text = text.Trim(' ');
If you instead want to remove all spaces from a string you could do:
text = text.Replace(" ", "");
I think maybe your code is something like this
Dim str As String = "إختر "
str.Replace(" ", "")
But actually you should
Dim str As String = "إختر "
str = str.Replace(" ", "")
I have just had a similar problem.
It turns out, that this nbsp character is Chr(160) from the ASCII table. Thus, something like this is quite meaningful, for all the cases. It works, on a selected area:
Public Sub remove_space_in_string()
Dim r_range As Range
For Each r_range In Selection
r_range = Trim(r_range)
r_range = Replace(r_range, vbTab, "")
r_range = Replace(r_range, " ", "")
r_range = Replace(r_range, Chr(160), "")
Next r_range
End Sub

How to get rid of the zero '0' numeric from a string?

BEFORE:
Johnson0, Yvonne
AFTER:
Johnson, Yvonne
String functions for Access can be found at http://www.techonthenet.com/access/functions/string/replace.php
In your example, code like
Replace("Johnson0", "0", "")
will do the trick for the particular string Johnson0. If you need to only remove the zero if it is the last character, play with the additional start and count parameters described in the link above.
You can try executing following query..
UPDATE table set
columnName = REPLACE(columnName,'0','')
WHERE columnName LIKE "%0%";
This will replace all occurrence of "0" with "".
The answer you submitted clarifies your requirement. Based on that, you don't need to create a user-defined function if your Access version is 2000 or later. You can get the same result with the Replace() function.
MsgBox Replace("Jonson0, Yvonne", "0,", ",")
One approach is to create a custom function
See http://www.techonthenet.com/access/functions/misc/alphanumeric.php for an example. You could do something similar, but in the loop you would only keep the alpha characters.
Public Sub xxx()
MsgBox RemoveStr0("Jonson0, Yvonne")
End Sub
Public Function RemoveStr0(sString As String) As String
Dim ipos As Long, sTemp As String
ipos = InStr(1, sString, "0,")
sTemp = Mid$(sString, 1, ipos - 1)
sTemp = sTemp & Mid$(sString, ipos + 1)
RemoveStr0 = sTemp
End Function
if you can pull it out to java or another OO lang you can just do a matching using regexes.

String Concatenation in VBA

I've got this function:
Function GetFullNameCSV() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".txt")
End Function
If the code looked like this hard coded:
Filename:= "C:\directory\filename.txt"
I could just replace it with this and get the same result:
Filename:= GetFullNameCSV()
Problem is it appears in the case of this string:
Connection:= "TEXT;C:\directory\filename.txt"
I need to use string concatenation, guessing something like this:
Connection:= "\"TEXT;+GetFullNameCSV()+\""
What the code I need to use? Thanks.
Assuming that all the sections are always delimited with a ;, just split the string into it's parts and then do your changes and then join it together again.
So with your sample I think it would be something like (please note, written from memory, so might not be completely correct):
Dim parts() as String
parts = Split(str, ";")
parts(2) = GetFullNameCSV() ' assuming that part 2 is the one you want to replace
str = Join(parts, ";")