I new to VB.NET coding. I have one question, kindly answer this.
I made a program which inserts current date into database. But when it displays the date, its format is dd/MM/yyyy ie. 01/01/2018. I want the date to display as A/A/BJAH where 1=A 2=B 3=C 4=D etc.
As i said in the comment, you would have to convert numbers into alphabets. This is a simple function to achieve what you are looking for:
Function EncryptDate(Datevalue As String) As String
Dim str As String = Datevalue
str = str.Replace(1, "A")
str = str.Replace(2, "B")
str = str.Replace(3, "C")
str = str.Replace(4, "D")
str = str.Replace(5, "E")
str = str.Replace(6, "F")
str = str.Replace(7, "G")
str = str.Replace(8, "H")
str = str.Replace(9, "I")
str = str.Replace(0, "J")
Return str
End Function
Thus MessageBox.Show(EncryptDate(Now.ToString("dd/MM/yyyy"))) will give
BI/JH/BJAG
Related
I have file with name All PM 7.6 10-Jun v2 , All PM 7.6 is a dynamic name and (10-Jun) is static name formulated using Format(Date, "DD-MMM")
I want to get these elements (All PM 7.6) ,I tried the following:
Dim arrname, strExisting As String
arrname = Split(ThisWorkbook.name, ".")
strExisting = arrname(0) & "." & arrname(1)
I think using arrname(0) & "." & arrname(1) is not the proper way to get it ,as if this parts of name changed then I need to modify the code again.
In advance, thanks for all your help.
Since you did not clarify (at least, for me) what is to be returned, please use the next function to extract the date string:
Function ExtractDateStr(strName As String) As String
Dim arr: arr = Split(strName, " ")
Dim strD As String: strD = arr(UBound(arr) - 1)
ExtractDateStr = strD
End Function
It can be used in the next way, to return the prefix before the date string or each element up to it:
Sub testExtractWbNameElements()
Dim x As String, strPrefix As String
x = "All PM 7.6 10-Jun v2.xlsx"
Debug.Print ExtractDateStr(x)
strPrefix = left(x, InStr(x, " " & ExtractDateStr(x)) - 1)
Debug.Print strPrefix 'the string before the date part
Dim arrElem
arrElem = Split(strPrefix, " ") 'each elements up to the date part, in an array:
Debug.Print UBound(arrElem), Join(arrElem, "|") 'just proving the return...
End Sub
The above solution assumes that the elements before the date part may be different in terms of number. If only three, as in your example, the solution can be simpler..
Edited:
If the first elements to be returned will always be three, use the next simpler way:
Sub testSplitSpecElemNo()
Dim x As String, arr
x = "All PM 7.6 10-Jun v2.xlsx"
arr = Split(x, , 4, 0) '4 means how many elements to return. First three and the last one (the rest all toghether) like the foourth
arr(UBound(arr)) = "$##%&" 'characters not very probable to exist like such a group in the other array elements...
arr = filter(arr, arr(UBound(arr)), False) 'eliminate the last global one
Debug.Print Join(arr, "|")
End Sub
I have an array of strings:
array()
and
string = "('one','two','three', ...)"
The values one two three are examples. The string may have more than three words.
How can I fill the array() with:
"one"
"two"
"three"
...
Dim s = "('one','two','three', ...)"
Dim tokens = From token In s.Trim("(", ")").Split(","c)
Select token.Trim("'"c)
Dim array() As String = tokens.ToArray()
Remember to add Imports System.Linq
Demo: https://dotnetfiddle.net/Ayy0y1
Note that it's not necessary to use LINQ, i just found it more readable than:
Dim array As String() = s.Trim("(", ")").Split(","c)
For i As Int32 = 0 To array.Length - 1
array(i) = array(i).Trim("'"c)
Next
Use String.Split to break up the starting string into an array populated with the split items.
Dim items = "one two three four five"
Dim array = items.Split(" ")
' Outputs array = [ "one", "two", "three", "four", "five" ]
Another solution is with the use of Regex:
Dim str As String = "('one','two','three')"
str = RegularExpressions.Regex.Replace(str, "\('|'\)", "")
Dim array() As String = RegularExpressions.Regex.Split(str, "','")
If there are spaces to remove then:
Dim str As String = "( 'one','two' , 'three ')"
str = RegularExpressions.Regex.Replace(str, "\s+|\(\s*'|'\s*\)", "")
Dim array() As String = RegularExpressions.Regex.Split(str, "','")
Start with the string as you presented it:
Dim myString As String = "('one','two','three')"
Debug.WriteLine(myString)
Output:
('one','two','three')
Next get rid of the unwanted characters by replacing them with empty quotes (nothingness). The Chr(39) function represents the single quote:
myString = myString.Replace("(", "").Replace(")", "").Replace(Chr(39), "")
Debug.WriteLine(myString)
output:
one,two,three
Next convert it to an array with the .Split() method:
Dim myStringArray() As String = myString.Split(",")
For i As Integer = 0 To myStringArray.Length - 1
Debug.WriteLine(myStringArray(i))
Next i
Output:
one
two
three
This works without any "Imports" statements
i've been working on this
Public Sub Main()
Dim str as String
str = Console.ReadLine()
str = str.ToLower()
str = str.Replace(" ","")
Dim mySort as Char() = str.ToCharArray()
System.Array.Sort(mySort)
For Each value As String In mySort
Console.Write(value)
Next
End Sub
if the input is United States of America
the result will be aaacdeeefiimnorsstttu
(it sorted alphabetically from a to z)
what I expect is uniittteeedssaaaofmrc
(sorted from the first character inputted from user followed by the duplicates (if there's any))
what should i do to have such expected result ?
Thanks
In reality your specification doesn't require any sorting but just a traversal of the elements (chars) of the string and the count of the elements of the same value present in the string
I would try with a GroupBy over the single characters and then a traversal in this way. During the iteration it is important to remove the characters found to not add them again to the resulting string
Public Sub Main()
Dim str As String
str = Console.ReadLine()
str = str.ToLower()
str = str.Replace(" ", "")
Dim myCharsCounter = str.GroupBy(Function(c) c).ToDictionary(Function(c) c.Key)
Dim result As String
For Each currentChar As Char In str
If myCharsCounter.ContainsKey(currentChar) Then
Dim charGroup = myCharsCounter(currentChar)
result &= New String(currentChar, charGroup.Count)
myCharsCounter.Remove(currentChar)
End If
Next
Console.WriteLine(result)
End Sub
Here is my solution, I tried running it on dotnetfiddle.net
Imports System
Public Module Module1
Public Sub Main()
Dim txt,trm as String
txt = Console.ReadLine()
trm = txt.ToLower().trim().Replace(" ","")
Dim chars as Char() = trm.ToCharArray()
Dim i as integer = 0
Dim j as integer = 0
Dim temp as string = ""
While (i < trm.Length)
j = i + 1
temp = temp + chars(i)
While ( j < trm.Length)
If (chars(i) = chars(j)) Then
temp = temp + chars(j)
chars(j)= " "
End If
j = j + 1
End While
i = i + 1
End While
temp = temp.Replace(" ","")
Console.WriteLine(temp)
End Sub
End Module
I am very new to Vb.net, and have found out how to split strings and put the remnants of the split string in an array like so...
Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next
Output:
vb.net
split
test
Now that I've got that figured out, I was wondering if there was a way to split on the SECOND space giving the output...
vb.net split
test
Thanks for your time!
You can do something like this
Dim input = "This is a Long String"
Dim splitted = input.Split(" "c)
Dim result = New List(Of String)()
For x As Integer = 0 To splitted.Length - 1 Step 2
result.Add(String.Join(" ", splitted.Skip(x).Take(2)))
Next
I was practicing to code anagram, but I find it easy since there is this method called Array.Sort().
Without using the method above, how do you sort them alphabetically? I just switched from Java to VB.net and still learning so yea :)
Assume this is the code:
Module Module1
Sub Main()
Dim str1, str2, str3 As String
Dim char1(), char2(), char3() As Char
Console.WriteLine("Enter 1st string:")
str1 = Console.ReadLine().ToLower.Replace(" ", "")
Console.WriteLine("Enter 1st string:")
str2 = Console.ReadLine().ToLower.Replace(" ", "")
Console.WriteLine("Enter 1st string:")
str3 = Console.ReadLine().ToLower.Replace(" ", "")
char1 = str1.ToCharArray()
char2 = str2.ToCharArray()
char3 = str3.ToCharArray()
End Sub End Module
If you can use Linq, you can call the OrderBy() extension on an array.
Here's some examples in C#.
sort string array using LINQ
'orderby' in linq using a string array c#
You might do something like this:
Dim ordered As Char() = char1.OrderBy(Function(x) x).ToArray()