I am trying to replace integers in a string using Visual Basic and can't seem to get it just right.
This is what I currently have:
lblNewPassword.Text = txtOrigPassword.Text.Replace("[0-9]", "Z")
I have also tried:
lblNewPassword.Text = txtOrigPassword.Text.Replace("#", "Z")
And:
lblNewPassword.Text = txtOrigPassword.Text.Replace("*#*", "Z")
You have to use a regex object like this:
C#
string input = "This is t3xt with n4mb3rs.";
Regex rgx = new Regex("[0-9]");
string result = rgx.Replace(input, "Z");
VB
Dim input As String = "This is t3xt with n4mb3rs."
Dim rgx As New Regex("[0-9]")
Dim result As String = rgx.Replace(input, "Z")
Update: if then you want to change vowels by X you can add:
rgx As New Regex("[A-Za-z]")
result = rgx.Replace(result, "X")
Related
I have a value I am capturing from an Http Request
Dim someValue As String = Request.Params("search")
Here is the value of my string:
?MyId1=VALUE1&MyId2=VALUE2&MyBoolen=True
I am trying to capture VALUE2. I tried the below code, but haven't had any success.
If Not String.IsNullOrEmpty(someValue) Then
Dim x = someValue.Substring(someValue.IndexOf("&"c) + 1)
If Not String.IsNullOrEmpty(someValue) Then
Dim y = x.Substring(someValue.IndexOf("="c) + 1)
End If
End If
How can I do this?
It looks like you're overthinking this. The Request object will let you look up the MyId2 value directly:
Dim MyId2 As String = Request.QueryString("MyId2")
It's also possible this is a nested query string, where what you actually have is something more like this:
/?search=%3FMyId1%3DVALUE1%26MyId2%3DVALUE2%26MyBoolen%3DTrue
This would give you the original string after the runtime URL Decodes the search element. In that case, you should look at the HttpUtility.ParseQueryString() method, rather than trying to do this yourself:
Dim search As String = Request.Params("search")
Dim searchValues As NameValueCollection = HttpUtility.ParseQueryString(search)
Dim MyId2 As String = searchValues("MyId2")
Which could even be written as a one-liner if we really wanted:
Dim MyId2 As String = HttpUtility.ParseQueryString(Request.Params("search"))("MyId2")
But if you really wanted to parse this by hand, one of the nice things about this is everything should be URL-encoded. This means you don't have worry about stray & or = characters as part of the data, and a simple Split() call should be safe:
Dim MyId2 As String = ""
Dim items = someValue.Substring(1).Split("&"c)
For Each item As String In Items
Dim parts = item.Split("="c)
If parts(0) = "MyId2" Then
MyId2 = parts(1)
Exit For
End If
Next
Or
Dim parts() As String = someValue.Substring(1).Split("&"c).
Select(Function(s) s.Split("="c)).
FirstOrDefault(Function(p) p(0) = "MyId2")
Dim MyId2 As String = If(parts IsNot Nothing, parts(1), "")
Is there a way to decode email subjects that are encoded? I know the dirty way of doing it is to get the string character between =?utf-8?B? xxx ?= and decoding that. But I have a program where I can get encoded strings like
=?utf-8?Bxxxx?= =?UTF-8?B?xxxx?= ...
Right now I'm doing something like this
If codedString.ToUpper().StartsWith("=?UTF-8?B?") Then
Dim temp As String = codedString.SubString(10)
Dim data = Convert.FromBase64String(temp)
Dim decodedString = ASCIIEncoding.ASCII.GetString(data)
'do something with decodedString
End If
But this doesn't work when the same string has multiple =?utf-8?B? encode like above. Also I can get strings with =?utf-8?Q encoding and =?windows-1252. Is there a way to tackle all of these encoding? I'm using Visual Studios 2017
I've never had trouble using this function to decode a email field value:
It finds matching utf-8 strings for types B or Q, and if type B, runs FromBase64String.
I'm sure you can manipulate for windows-1252.
Private Function DecodeEmailField(byVal strString as String) as String
DecodeEmailField = strString.toString()
Dim strMatch
Dim arrEncodeTypes = New String() {"B","Q"}
Dim strEncodeType as String
For Each strEncodeType in arrEncodeTypes
Dim objRegexB as RegEx = new RegEx("(?:\=\?utf\-8\?" & strEncodeType & "\?)(?:.+?)(?:\?=\s)", _
RegexOptions.Multiline or RegexOptions.IgnoreCase)
if (objRegexB.IsMatch(DecodeEmailField)) then
Dim thisMatch as Match = objRegexB.Match(DecodeEmailField)
For Each strMatch in thisMatch.Groups
Dim strMatchHold as String = strMatch.toString().Substring(("=?utf-8?" & strEncodeType & "?").length)
strMatchHold = strMatchHold.SubString(0,(strMatchHold.Length)-("?= ".Length))
If strEncodeType = "B" Then
Dim data() As Byte = System.Convert.FromBase64String(strMatchHold)
strMatchHold = System.Text.UTF8Encoding.UTF8.GetString(data)
End If
DecodeEmailField = Replace(DecodeEmailField,strMatch.toString(),strMatchHold)
Next
End If
Next
End Function
I have the following Objective-C code:
if(accountNumber!=nil)
if(accountNumber!=nil){
a few lines down:
if(orderDate!=nil)
if(orderDate!=nil){
so my question is this: is there a regex that will replace the 2nd line of text with just the curly brace? the "{"...
this is a piece of generated code and I don't have the generator so I have to massage the output and this is one of the pain points I get to look for...
stribizhev's solution worked
Private Function RunOtherChanges(ByVal str As String) As String
Dim pattern As String = "(if\(\w+!=nil\))\s*\1\s*{"
Dim replacement As String = "$1 {"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)
Return result
End Function
i have textbox with text is "ab1234de524sfe6985dsef456" and i want take all number in this to array
like
arr(0)=1234,
arr(1)=524,
arr(2)=6985,
arr(3)=456
help me, thanks
You can use Regex.Check out following code snippet that is written in c#
string strRegex = #"\d+";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = #"ab1234de524sfe6985dsef456";
ArrayList list=new ArrayList();
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
list.Add(myMatch.Value);
}
}
list.Dump();
Converted to Visual Basic:
Dim strRegex As String = "\d+"
Dim myRegex As New Regex(strRegex, RegexOptions.Singleline)
Dim strTargetString As String = "ab1234de524sfe6985dsef456"
Dim list As New ArrayList()
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
list.Add(myMatch.Value)
End If
Next
I have a concatenated string like
str = 1010,5050,6079
I want to separate the string. In JavaScript, I can do
string = Srt.split(","):
and get the result
string[2]=5050
Is there a way to do this in VB.NET?
It works the same in VB.NET
Dim input As String = "1010,5050,6079"
Dim result As String() = input.Split(",")
result(0) //contains 1010
result(1) //contains 5050
result(2) //contains 6079
There is no diference. Try with the same code in VB.NET:
Dim shoppingList As String = "Milk,Eggs,Bread"
Dim shoppingItem(3) As String
shoppingItem = shoppingList.Split(",")