Sort Char without using Array.Sort() - VB.net - arrays

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()

Related

Equivalent of Visual Basic Split() in Attachmate Extra! Basic?

I'm working with an Attachmate Extra Basic Macro and need to Split() a string. I've tried several variations with Extra! Basic and it seems there is no Split() option. The below code works in Visual Basic; how would I do the same thing in Extra! Basic?
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

adding characters to string vb.net

The ending of the student name should read "2016". Is this the best way to modify the string:
Dim Student As String
If Student.Substring(0, 8) = "JoeBlogs" Then
stg = Student.Insert(0, 3)("2016")
End If
I want the string to read "Joe2016Blogs"
I recommend following the advice in Fabio's answer, however, to answer your specific question, you can do the following:
Dim Student As String = "JoeBlogs"
If Student.Substring(0) = "JoeBlogs" Then
Dim stg as string = Student.Insert(3, "2016")
console.WriteLine(stg)
End If
Which produces the following:
Joe2016Blogs
String concatenation should be avoided when done many thousands of times, but for something quick the following will do it: stg = Student & "2016"
String type is immutable, so in any case you need to create new string.
You can use concatenation
Dim newValue As String = stringVariable + "2016" + anotherStringVariable
or you can use String.Format method
Dim newValue As String = String.Format("{0}2016{1}", firstValue, secondValue);
In VB.NET 14 you can use more readable string interpolation feature
Dim newValue As String = $"{first}2016{second}"
And if you creating string in the loops with unknown amount of variables use StringBuilder
Dim builder As New StringBuilder()
For Each item As String in stringCollection
builder.Append(item)
builder.Append("2016")
End For
Dim allItems As String = builder.ToString()
In your case main problem is to split "JoeBlog" to the name and "blog" word and then put "2016" between

Type casting Strings

I am using an array list to store my strings. However when I run the program it throws an error : Conversion from type 'String()' to type 'String" is not valid. I am sure I am missing something simple. I thought an ArrayList was a list of objects and that it could be anything that I was sent to the array. e.g. Strings in, Strings out. Here is the code that I am working with.
Dim tempString As New ArrayList()
For Each s As String In tempString
Dim sAry As String() = bufString.Split(New Char() {ChrW(2)})
For i As Int16 = 1 To sAry.Length
xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
Next
Next
I also have come to understand that ArrayList has depreciated, should I be using something else?
Before jumping into your case quickly please give a look into other great answers
What's Wrong with an ArrayList?
.NET: ArrayList vs List
and you can google to know more in this regard...
For known typed collection it is not good to use ArrayLists.
For your case I would suggest generic List collection(List is much more efficient in terms of memory usage)
You can see my below test case for you:
Sub Main()
Dim tempList As New List(Of String) 'Consider List Rather ArrayList as you knw your requirement for Strings collection
tempList.Add("string1string2string3string4") 'for testing purpose I've joined "string1, string2, string3 & string4" with ChrW(2) unicode character
For Each s As String In tempList
Dim sAry As String() = s.Split(New Char() {ChrW(2)})
For i As Int16 = 1 To sAry.Length
'xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
Console.WriteLine(sAry(i - 1).ToString())
Next
Next
Console.ReadLine()
End Sub

VB.NET How to declare new empty array of known length

Is there a way in VB.NET to declare an array, and later initialize it to a known length in the code? In other words, I'm looking for the VB.NET equivalent of the following C#.NET code:
string[] dest;
// more code here
dest = new string[src.Length];
I tried this in VB, and it didn't work.
Dim dest() as string
' more code here
dest = New String(src.Length)
What am I missing?
NOTE: I can confirm that
Dim dest(src.Length) as string
works, but is not what I want, since I'm looking to separate the declaration and initialization of the array.
The syntax of VB.NET in such a case is a little different. The equivalent of
string[] dest;
// more code here
dest = new string[src.Length];
is
Dim dest As String()
' more code here
dest = New String(src.Length - 1) {}
Syntax note
When you use Visual Basic syntax to define the size of an array, you specify its highest index, not the total number of elements in the array. learn.microsoft.com
Example
These two array both have a length of 5:
C#:
string[] a = new string[5];
VB:
Dim a As String() = New String(4) {}
The normal way to do this would be to declare the array like so:-
Dim my_array() As String
and later in the code
ReDim my_array (src.Length - 1)
You can use Redim as already noted but this is the equivalent VB code to your C#
Dim dest As String()
dest = New String(src.Length - 1) {}
Try and avoid using dynamic arrays though. A generic List(Of T) is much more flexible

How to assign a set of text values to a string array?

How can I assign a set of text values to an array? Nothing I tried is working!
Months = Array("Jan", "Feb", ..., "Dec")
and others I tried do not work!
Here's something about VB: http://www.devx.com/vb2themax/Tip/18322
Visual Basic doesn't provide any way
to declare an array and initialize its
elements at the same time. In most
cases you end up with setting
individual elements one by one, as in:
Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
strArray(3) = "Winter"
Under VB4, VB5, and VB6 you can create
an array of Variants on the fly, using
the Array() function:
Dim varArray() As Variant
varArray() = Array("Spring", "Summer", "Fall", "Winter")
but there is no similar function to
create arrays of data types other than
Variant. If you're using VB6, however,
you can create String arrays using the
Split() function:
Dim varArray() As String
' arrays returned by Split are always zero-based
varArray() = Split("Spring;Summer;Fall;Winter", ";")
I'm pretty sure you can only do it like this:
dim months(2) as string
months(0) = "Jan"
months(1) = "Feb"
months(2) = "Mar"
If you're talking about vbscript then this works:
months = Array("may","june","july")
If it's vb.net then:
dim months() as string = {"may","june","july"}

Resources