Sunday, 17 February 2013

SQL SERVER DATATYPES and .NET Framework Datatypes mapping

SQL SERVER DATATYPES and .NET Framework Datatypes mapping

datetime
SQL SERVER DATA TYPES.NET CLR DATATYPES
bigintint64
intint32
smallintint16
tinyintbyte
decimalSqlDecimal
numericSqlDecimal
floatdouble
realshort
smallmoneydecimal
moneydecimal
datedatetime
datetimedatetime
timetimespan
smalldatetimedatetime
datetime2datetime
datetimeoffsetdatetimeoffset


tags: SQL DATA TYPES mapping in .NET Data types,SQL Data types  equivalent in C#,SQL Data types  equivalent in Vb.net, SQL Data types and .net data types mapping.

Friday, 15 February 2013

String split by Newline in VB.NET


String split by Newline in VB.NET

if String is in this format
     Dim  str as String = "1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN"

Split string by new line -> then  by  ')'  get exact Strings 


1) Split by Newline
            User can split by  "\r\n"   or System.Environment.NewLine

        Dim final As String = String.Empty
           Dim array() As String = str.Split(New String() {"\r\n"}
, StringSplitOptions.RemoveEmptyEntries)
           Dim s As String
           For Each s In array
               Console.WriteLine(s)
               final+=s.Split(New Char()
               {
                   ")"c
               }
, StringSplitOptions.RemoveEmptyEntries)(1)+" "
           Next
 
 OUTPUT
1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN
 
2) Split String by  ')'  character

            Dim finalwords() As String = final.Split(New String() {" "}
, StringSplitOptions.RemoveEmptyEntries)
           Array.Sort(finalwords)
           Dim i As Integer =  0
           Dim f As String
           For Each f In finalwords
               Console.Write(f + "\t")
           Next

FINAL OUTPUT:
ABS SIGN MOD FLOOR CEIL ROUND EXP LOG LOG10 POW POWER SQRT PI COS SIN TAN
ABS     CEIL    COS     EXP     FLOOR   LOG     LOG10   MOD     PI      POW
POWER   ROUND   SIGN    SIN     SQRT    TAN

Tags:String split by Newline in VB.NET,String split by '\r\n',String Split by System.Environment.NewLine. Split Strings in VB.NET,Split a String on Line Breaks,Split a String on carriage Return

String split by Newline in C#

String split by Newline in C#

if String is in this format
          String str = @"1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN";

Split string by new line -> then  by  ')'  get exact Strings 


1) Split by Newline
            User can split by  "\r\n"   or System.Environment.NewLine

          String final=String.Empty;
           String[] array=str.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
           foreach (String s in array)
           {
               Console.WriteLine("--"+s+"--");
               final+=s.Split(new char[] {')'}, StringSplitOptions.RemoveEmptyEntries)[1]+" ";
           }

OUTPUT
1)ABS
2)SIGN
3)MOD
4)FLOOR
5)CEIL
6)ROUND
7)EXP
8)LOG
9)LOG10
10)POW
11)POWER
12)SQRT
13)PI
14)COS
15)SIN
16)TAN
 
2) Split String by  ')'  character

           String[] finalwords=final.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
           Array.Sort(finalwords);
           int i = 0;
           foreach (String f in finalwords)
           {
               Console.Write(f + "\t");
           }

FINAL OUTPUT:

ABS SIGN MOD FLOOR CEIL ROUND EXP LOG LOG10 POW POWER SQRT PI COS SIN TAN
ABS     CEIL    COS     EXP     FLOOR   LOG     LOG10   MOD     PI      POW
POWER   ROUND   SIGN    SIN     SQRT    TAN

Tags:String split by Newline in C#,String split by '\r\n',String Split by System.Environment.NewLine. Split Strings in C#,Split a String on Line Breaks,Split a String on carriage Return

Tuesday, 12 February 2013

Using ArrayList in VB.NET


Using ArrayList in  VB.NET

This example explains how to insert items into array list and fetch them based on type.

ArrayList is used for dynamic length 

Add namespace  System.Collections;

Create an Array List

Dim arrayList As ArrayList =  New ArrayList()

Add items to an array list


            arrayList.Add(1)
            arrayList.Add(2)
            arrayList.Add(3)
            arrayList.Add(1.1f)
            arrayList.Add(0.5f)
            arrayList.Add("Hello")
            arrayList.Add("World!")

It has integers and float values and string values also/

Get All values from Array List

            Console.WriteLine("Display All Values in Array List")
            Dim item As var
            For Each item In arrayList
                Console.Write(item+"\t")
            Next
Output: 1       2       3       1.1     0.5 Hello World!

Get Only Integers from Array List

            Console.WriteLine("Get Only Integers")
            Dim item As var
            For Each item In arrayList.OfType<int>()

                Console.Write(item+"\t")
            Next
Output:1       2       3
Get Only Float values from Array List

            Console.WriteLine("Get Only float")
            Dim item As var
            For Each item In arrayList.OfType<float>()

                Console.Write(item+"\t")
            Next
Output:1.1 0.5
Get Only String values from Array List

            Console.WriteLine("Get Only String Values")
            Dim item As var
            For Each item In arrayList.OfType<String>()

                Console.Write(item+"\t")
            Next
Output:Hello World!

Tags: using ArrayList in VB.NET,VB.NET ArrayList,ArrayList in VB.NET, ArrayList OfType method usage,Insert elements into Array List, Dynamic Array in VB.NET

Using ArrayList in C#

Using ArrayList in C#

This example explains how to insert items into array list and fetch them based on type.

ArrayList is used for dynamic length 

Add namespace  System.Collections;

Create an Array List

ArrayList arrayList = new ArrayList();

Add items to an array list

            arrayList.Add(1);
            arrayList.Add(2);
            arrayList.Add(3);
            arrayList.Add(1.1f);
            arrayList.Add(0.5f);
            arrayList.Add("Hello");
            arrayList.Add("World!");
It has integers and float values and string values also/

Get All values from Array List

            Console.WriteLine("Display All Values in Array List");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
Output: 1       2       3       1.1     0.5 Hello World!

Get Only Integers from Array List

            Console.WriteLine("Get Only Integers");
            foreach (var item in arrayList.OfType<int>())
            {
               
                Console.Write(item+"\t");
            }
Output:1       2       3

Get Only Float values from Array List

            Console.WriteLine("Get Only float");
            foreach (var item in arrayList.OfType<float>())
            {
               
                Console.Write(item+"\t");
            }
Output:1.1 0.5

Get Only String values from Array List

            Console.WriteLine("Get Only String Values");
            foreach (var item in arrayList.OfType<string>())
            {
               
                Console.Write(item+"\t");
            }
Output:Hello World!

Tags: using ArrayList in C#,C# ArrayList,ArrayList in C#, ArrayList OfType method usage,Insert elements into Array List, Dynamic Array in C#

Monday, 11 February 2013

VB.NET Remove duplicate numbers/records from an array


  VB.NET Remove duplicate numbers/records from an array


Suppose array has series of numbers, if user wants to extract/find duplicated number in an array.
Here is the Logic.
Private Shared Sub RemoveDuplicateNumbersInArray()
    Dim a As Integer() = New Integer() {10, 20, 10, 30, 30, 20, _
        40, 40, 12, 14, 12, 14}

    Dim query =


    a = New Integer(query.Count() - 1) {}
    For i As Integer = 0 To query.Count() - 1
        a(i) = query.ElementAt(i).Key
    Next

    For Each i As Integer In a
        Console.WriteLine(i)
    Next
End Sub
Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.if any group has more than one element          
                    query.Count() returns unique elements count.
            3. Then assign this values to existing array.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:10      20      100     30      40      50      12      14


i.e 10,20,100 repeated more than once . so these are extra values removed from the array.

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 10      20      30      40      12      14
 i.e 10 ,20,30,40,12,14 numbers repeated more than one time in an array a.

Tags:
  VB.NEt Remove duplicate numbers/records from an array,vb.net Remove duplicate records from an array,vb.net get unique elements array,vb.net get unique array,IGrouping LINQ,LINQ Count,LINQ Select on IGrouping,vb.net Find only Duplicate Values in an Array using LINQ

C# Remove duplicate numbers/records from an array

C# Remove duplicate numbers/records from an array


Suppose array has series of numbers, if user wants to extract/find duplicated number in an array.
Here is the Logic.

static void RemoveDuplicateNumbersInArray()
        {
            int[] a = new int[] { 10, 20, 10, 30, 30, 20, 40, 40, 12, 14, 12, 14 };





            var query = from d in a
                        group d by d into da
                        select da;


            a = new int[query.Count()];
            for (int i = 0; i < query.Count(); i++)
            {
                a[i] = query.ElementAt(i).Key;
            }

            foreach (int i in a)
            {
                Console.WriteLine(i);
            }
      }

Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.if any group has more than one element          
                    query.Count() returns unique elements count.
            3. Then assign this values to existing array.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:10      20      100     30      40      50      12      14


i.e 10,20,100 repeated more than once . so these are extra values removed from the array.

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 10      20      30      40      12      14
 i.e 10 ,20,30,40,12,14 numbers repeated more than one time in an array a.

Tags:
C# Remove duplicate numbers/records from an array,C# Remove duplicate records from an array,C# get unique elements array,C# get unique array,IGrouping LINQ,LINQ Count,LINQ Select on IGrouping,C# Find only Duplicate Values in an Array using LINQ

VB.NET Find only Duplicate Values in an Array


VB.NET Find only Duplicate Values in an Array

Suppose array has series of numbers, if user wants to extract/find duplicated number in an array.
Here is the Logic.


Private Shared Sub GetDuplicateElementsInArray()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =

    For Each i As var In query.[Select](Function(ab, bc) ab).Where(Function(ab, bc) ab.Count() <> 1)
        Console.WriteLine(i.Key)
    Next



End Sub

Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.if any group has more than one element          
                    query.Select((ab, bc) => ab.Count()) will be be greater than 1.
            3. Then filter the groups which has counter != 1  in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:
10
20
100



i.e 10,20,100 repeated more than once . so these are duplicate values in an array.

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 10
20
30
40
12
14
 i.e 10 ,20,30,40,12,14 numbers repeated more than one time in an array a.

Tags:
VB.NET  Find only Duplicate Values in an Array,get repeated elements in an array,get non-unique values from an array,IGrouping LINQ,LINQ Count,LINQ Select on IGrouping,C# Find only Duplicate Values in an Array using LINQ

C# Find only Duplicate Values in an Array

C# Find only Duplicate Values in an Array

Suppose array has series of numbers, if user wants to extract/find duplicated number in an array.
Here is the Logic.


      static void GetDuplicateElementsInArray()
        {
            int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };

              



               var query = from d in a
                           group d by d into da
                           select da;

               foreach (var i in query.Select((ab, bc) => ab).Where((ab, bc) => ab.Count() != 1))
                   Console.WriteLine(i.Key);

         

        }

Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.if any group has more than one element          
                    query.Select((ab, bc) => ab.Count()) will be be greater than 1.
            3. Then filter the groups which has counter != 1  in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output
10
20
100



i.e 10,20,100 repeated more than once . so these are duplicate values in an array.

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 10
20
30
40
12
14
 i.e 10 ,20,30,40,12,14 numbers repeated more than one time in an array a.

Tags:
C# Find only Duplicate Values in an Array,get repeated elements in an array,get non-unique values from an array,IGrouping LINQ,LINQ Count,LINQ Select on IGrouping,

find least repeated number in an array VB.NET


find least repeated number in an array VB.NET using LINQ


Suppose array has series of numbers, if user wants to find maximum repeated number in an array.



Private Shared Sub FindLeastRepeatedNumbers()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =


    Dim min As Integer = query.[Select](Function(ab, bc) ab.Count()).Min()

    Console.WriteLine(min)
    For Each i As var In query.[Select](Function(ab, bc) ab).Where(Function(ab, bc) ab.Count() = min)
        Console.WriteLine(i.Key)
    Next
End Sub

Note
:  1.Group each number by that number, so that duplicates will be under same group.
            2.Get Which group has maximum elements           
                   int max = query.Select((ab, bc) => ab.Count()).Min();
            3. Then filter the groups which has min count got in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:  1
30
40
50
12
14

i.e one time repeated numbers in array a

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 2
10
20
30
40
12
14
 i.e 10 ,20,30,40,12,14 numbers repeated 2 times in an array.






Tags:VB.NET find least repeated number in an array, Find repeated numbers in an array,Find duplicate numbers in an array, ,find least repeated number in an array

find least repeated number in an array C#


find least repeated number in an array C#


Suppose array has series of numbers, if user wants to find maximum repeated number in an array.



 static void FindLeastRepeatedNumbers()
        {
            int[] a = new int[] { 10, 20,100,100,100, 10, 30, 20, 40, 50, 12, 14 };

            var query = from d in a
                        group d by d into da
                        select da;


            int min = query.Select((ab, bc) => ab.Count()).Min();

            Console.WriteLine(min);
            foreach (var i in query.Select((ab, bc) => ab).Where((ab, bc) => ab.Count() == min))
                Console.WriteLine(i.Key);
        }

Note
:  1.Group each number by that number, so that duplicates will be under same group.
            2.Get Which group has maximum elements            
                   int max = query.Select((ab, bc) => ab.Count()).Min();
            3. Then filter the groups which has min count got in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:  1
30
40
50
12
14

i.e one time repeated numbers in array a

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 2
10
20
30
40
12
14
 i.e 10 ,20,30,40,12,14 numbers repeated 2 times in an array.






Tags:C# find least repeated number in an array, Find repeated numbers in an array,Find duplicate numbers in an array, ,find least repeated number in an array

C# find maximum repeated number in an array

C#  find maximum repeated number in an array


Suppose array has series of numbers, if user wants to find maximum repeated number in an array.



        static void FindMaximumRepeatedNumbers()
        {
            int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };

            var query = from d in a
                        group d by d into da
                        select da;


            int max = query.Select((ab, bc) => ab.Count()).Max();

            Console.WriteLine(max);
            foreach (var i in query.Select((ab, bc) => ab).Where((ab, bc) => ab.Count() == max))
                Console.WriteLine(i.Key);
        }

Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.Get Which group has maximum elements             
                   int max = query.Select((ab, bc) => ab.Count()).Max();
            3. Then filter the groups which has max count got in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:  3-100 (i.e 100 repeated 3 times)
Input:  int[] a = new int[] { 10, 20,  10, 30, 20, 40, 50, 12, 14 };
Output: 2
10
20
 i.e 10 and 20 numbers repeated 2 times in an array.






Tags:C# find maximum repeated number in an array, Find repeated numbers in an array,Find duplicate numbers in an array, ,find least repeated number in an array

VB.NET find maximum repeated number in an array

find maximum repeated number in an array VB.NET


Suppose array has series of numbers, if user wants to find maximum repeated number in an array.



Private Shared Sub FindMaximumRepeatedNumbers()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =


    Dim max As Integer = query.[Select](Function(ab, bc) ab.Count()).Max()

    Console.WriteLine(max)
    For Each i As var In query.[Select](Function(ab, bc) ab).Where(Function(ab, bc) ab.Count() = max)
        Console.WriteLine(i.Key)
    Next
End Sub


Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.Get Which group has maximum elements             
                   int max = query.Select((ab, bc) => ab.Count()).Max();
            3. Then filter the groups which has max count got in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:  3-100 (i.e 100 repeated 3 times)
Input:  int[] a = new int[] { 10, 20,  10, 30, 20, 40, 50, 12, 14 };
Output: 2
10
20
 i.e 10 and 20 numbers repeated 2 times in an array.






Tags:VB.NET find maximum repeated number in an array, Find repeated numbers in an array,Find duplicate numbers in an array, ,find least repeated number in an array

VB.NET Find repeated numbers in an array

VB.NET  Find repeated numbers in an array

Integer Array has  numbers ,some are repeated, if you want to find which number is repeated how many time., Here is the logic using LINQ.
Private Shared Sub FindRepeatedNumbers()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =

    Dim maximum As Integer = a.Max()
    For Each intagroup As IGrouping(Of Integer, Integer) In query
        Console.WriteLine("Key={0},Repeated {1} Times", intagroup.Key, intagroup.Count())
    Next
End Sub

Note:
  1. First group by  each number
  2. there will a unique groups ,then find each group has how many numbers.
Here is the output.

Number=10,Repeated 2 Times
Number=20,Repeated 2 Times
Number=100,Repeated 3 Times
Number=30,Repeated 1 Times
Number=40,Repeated 1 Times
Number=50,Repeated 1 Times
Number=12,Repeated 1 Times
Number=14,Repeated 1 Times

Tags: VB.NET Find repeated numbers in an array,Find duplicate numbers in an array, find maximum repeated number in an array,find least repeated number in an array

C# Find repeated numbers in an array

C# find repeated numbers in an array


Integer Array has  numbers ,some are repeated, if you want to find which number is repeated how many time., Here is the logic using LINQ.

       static   void FindRepeatedNumbers()
        {
               int[] a=new int[]{10,20,100,100,100,10,30,20,40,50,12,14};

               var query = from d in a
                           group d by d into da
                           select da;

              int maximum = a.Max();
               foreach (IGrouping<int, int> intagroup in query)
               {
                   Console.WriteLine("Key={0},Repeated {1} Times",intagroup.Key,intagroup.Count());
               }
   }

  1. First group by  each number
  2. there will a unique groups ,then find each group has how many numbers.
Here is the output.

Number=10,Repeated 2 Times
Number=20,Repeated 2 Times
Number=100,Repeated 3 Times
Number=30,Repeated 1 Times
Number=40,Repeated 1 Times
Number=50,Repeated 1 Times
Number=12,Repeated 1 Times
Number=14,Repeated 1 Times

Tags: C# Find repeated numbers in an array,Find duplicate numbers in an array, find maximum repeated number in an array,find least repeated number in an array

Sunday, 10 February 2013

String Reverse in VB.NET using LINQ

String Reverse in VB.NET using LINQ


String reverse using LINQ in VB.NET
Reversing a string :String class has reverse method,
this Reverse method returns IEnumerable<char> so you need to convert it to an array of character.
String constructor accepts  array of characters as input param.

Shared Function StringReverse() As String
          Dim str As String =  "Hello World!"
          Console.WriteLine(str)
          str = New String(str.Reverse().ToArray())
          Console.WriteLine(str)
           Return str
 End Function

Output:  !dlroW olleH

Tags:String Reverse in VB.NET using LINQ, String.Reverse(),convert characters to an array,convert an array of characters to String,String reverse using LINQ,Reverse.ToArray()



String Reverse in C# using LINQ

String Reverse in C#


String reverse using LINQ in C#
Reversing a string :String class has reverse method,
this Reverse method returns IEnumerable<char> so you need to convert it to an array of character.
String constructor accepts  array of characters as input param.

static String StringReverse()
      {
          String str = "Hello World!";
          Console.WriteLine(str);
          str = new string(str.Reverse().ToArray());
          Console.WriteLine(str);
           return str;
      }

Output:  !dlroW olleH

Tags:String Reverse in C# using LINQ, String.Reverse(),convert characters to an array,convert an array of characters to String,String reverse using LINQ,Reverse.ToArray()

get numbers from string VB.NET Regular Expressions

get numbers from string  using Regular Expressions Vb.net
/get numbers from string VB.NET

Suppose String is Alphanumeric, if user wants to extract Numbers only, in that case you can use Regular expressions to match digit patterns.

for ex: String str = "1abc34bcd677bsgdb7878ddh63d4d";
output: 
1
34
677
787
63

Regular Expression for matching digit is "\d", + means one or more digits(\d ).
Shared  Sub GetAllNumbersFromString()
Dim str As String =  "1abc34bcd677bsgdb7878ddh63d4d"

Dim mc As MatchCollection = Regex.Matches(str,"\d+")
Dim i As Integer
For  i = 0 To  mc.Count- 1  Step  i 1
Console.WriteLine(mc(i).Value)
Next
 End Sub
Note: One way of getting Numbers from a String

Tags:get numbers from string VB.NET Regular Expressions,
get numbers from string VB.NET,Regex.Matches.MatchCollection,Integer Arrays VB.NET,Regular Expression Pattern matching digits only.

get numbers from string c# Regular Expressions

get numbers from string c# Regular Expressions/get numbers from string c#

Suppose String is Alphanumeric, if user wants to extract Numbers only, in that case you can use Regular expressions to match digit patterns.

for ex: String str = "1abc34bcd677bsgdb7878ddh63d4d";
output: 
1
34
677
787
63

static void GetAllNumbersFromString()
{
String str = "1abc34bcd677bsgdb7878ddh63d4d";

MatchCollection mc=Regex.Matches(str, @"\d+");
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Value);
}
}

Note: One way of getting Numbers from a String

Tags:get numbers from string c# Regular Expressions,
get numbers from string c#,Regex.Matches.MatchCollection,Integer Arrays C#,Regular Expression Pattern matching digits only.

Saturday, 9 February 2013

sort the array with even first and then odd VB.NET


sort the array with even first and then odd VB.NET

If array contains Even and odd numbers, first group numbers by even and odd and then sort each group ascending or descending order.  merge each group with other. it can be even numbers first/odd numbers array.
for ex:             Dim intArr() As Integer =  New Integer() {10,2,3,4,5,6,7,8,9,1}
 
output: 10      8       6       4       2       9       7       5       3       1
i.e even numbers first in descending order then odd numbers next in descending order.

Here is an example.


 Private  Sub SortEvenandOddNumbersInDescendingOrder()
            Dim intArr() As Integer =  New Integer() {10,2,3,4,5,6,7,8,9,1}


            var query = from i in intArr
                        group i by (i % 2 = 0) into gevenodd

                        Dim gevenodd As select

           Dim EvenArray As var =  query.FirstOrDefault().OrderByDescending(d = >d)
           Dim OddArray As var =  query.LastOrDefault().OrderByDescending(d = >d)

           Dim num As var
           For Each num In EvenArray
               Console.WriteLine(num)
           Next

           Dim num As var
           For Each num In OddArray
               Console.WriteLine(num)
           Next


        Dim joinarrays As var = EvenArray.Union(OddArray)

          Console.WriteLine("final output")

          Dim a As var
          For Each a In joinarrays

              Console.Write(a+"\t")
          Next
          intArr=joinarrays.ToArray()

          Dim i As Integer
          For  i = 0 To  intArr.Length- 1  Step  i + 1
              Console.Write(intArr(i) + "\t")
          Next
 End Sub
 
 OUTPUT:

10
8
6
4
2
9
7
5
3
1
final output
10      8       6       4       2       9       7       5       3       1
10      8       6       4       2       9       7       5       3       1
 
Tags:  sort the array with even first and then odd VB.NET,sorting even numbers array,sorting odd numbers array, LINQ union operator, LINQ group by operator,Sorting an array using LINQ,LINQ
OrderByDescending ,LINQ OrderBy.

sort the array with even first and then odd C#

sort the array with even first and then odd C#

If array contains Even and odd numbers, first group numbers by even and odd and then sort each group ascending or descending order.  merge each group with other. it can be even numbers first/odd numbers array.

for ex:             int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};
 
output: 10      8       6       4       2       9       7       5       3       1

i.e even numbers first in descending order then odd numbers next in descending order.

Here is an example.

void SortEvenandOddNumbersInDescendingOrder()
{
            int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;

           var EvenArray= query.FirstOrDefault().OrderByDescending(d=>d);
           var OddArray = query.LastOrDefault().OrderByDescending(d=>d);

           foreach (var num in EvenArray)
           {
               Console.WriteLine(num);
           }

           foreach (var num in OddArray)
           {
               Console.WriteLine(num);
           }


        var joinarrays  =EvenArray.Union(OddArray);

          Console.WriteLine("final output");

          foreach (var a in joinarrays)
          {
             
              Console.Write(a+"\t");
          }
          intArr=joinarrays.ToArray();

          for (int i = 0; i < intArr.Length; i++)
          {
              Console.Write(intArr[i] + "\t");
          }
        }
     
}

OUTPUT:

10
8
6
4
2
9
7
5
3
1
final output
10      8       6       4       2       9       7       5       3       1
10      8       6       4       2       9       7       5       3       1


Tags:
sort the array with even first and then odd C#,sorting even numbers array,sorting odd numbers array, LINQ union operator, LINQ group by operator,Sorting an array using LINQ,LINQ
OrderByDescending ,LINQ OrderBy.

get even numbers and odd numbers from an array C#

get even numbers and odd numbers from an array C#

Gets even and odd arrays from an integer array.

Example 1: 

           Group array elements by even or odd. using condition i % 2==0.  i.,e there will a 2 groups
if condition is true number will be in Even group, if condition is not true number will be in Odd group.


 static void EVENODDGROUP()
        {
            int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;

            foreach (var va in query)
            {
                Console.WriteLine("key={0}",va.Key);

                foreach (var v in va)
                {
                    Console.Write(v); Console.Write("\t");
                }

            }
        }


output:
key=Even group
10      2       4       6       8
key=Odd Group
3       5       7       9       1


Example 1: Get Even Array

  If condition is true then using ToArray() method get even numbers
 
            int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;
            foreach (var va in query)
            {
                if (va.Key == true)
                {

                    int[] EvenArray = va.ToArray();
foreach (var v in EvenArray)
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
                }
            }

OUTPUT:10      2       4       6       8

Example 1: Get Odd Array

  If condition is true then using ToArray() method get odd numbers
           
int[] intArr = new int[]{10,2,3,4,5,6,7,8,9,1};

            var query = from i in intArr
                        group i by (i % 2 == 0) into gevenodd
                    
                        select gevenodd;

            foreach (var va in query)
            {
                if (va.Key == false)                {
                    int[] OddArray = va.ToArray();
                    foreach (var v in OddArray)
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
                }
            }

           
        }
   
 
OUTPUT:3       5       7       9       1

Note:  User can sort Even or add arrays by using 

                    var EvenArray = va.OrderBy(v => v);
                    foreach (var v in EvenArray) //OddArray
                    {
                        Console.Write(v); Console.Write("\t");
                    }
                    Console.WriteLine();
 
Tags: Get Even odd arrays from an integer array. ToArray extension method, i%2==0,integer arrays, linq group by, linq order by,sort even arrays,sort odd arrays.

VB.NET split strings by length


VB.NET split strings by length

String methods are available to split strings by delimiters.  If user wants to split by length and last remaining portion of the string may may not be of the same length as user specified.

for ex: String str = "a ab abc abcd abcd abcdcd";

Split by 3    -> last string will be   "d " only(1char)


Here is  a method to Split a String by Length.


  Shared  Sub SplitStringsByLength(ByVal len As Integer)
          Console.WriteLine("SplitStringsByLength")
          Dim str As String =  "a ab abc abcd abcd abcdcd"

          While Not String.IsNullOrEmpty(str)

              Try
                  Console.WriteLine(str.Substring(0, len))
                  str = str.Remove(0, len)
              Catch ex As Exception
                  Dim break As Console.WriteLine("--"+str+"--")
              End Try
          End While
End Sub


call:  SplitStringsByLength(3);

OUTPUT

-a a-
-b a-
-bc -
-abc-
-d a-
-bcd-
- ab-
-cdc-
--d--



Note: String substr may through exception if string length is less than the length user specified,in that case get remaining portion of the string and break the loop.


Split a String by length using Regular Expression

User can split a string by using regular expressions.

suppose user's input:  str = "1111222233334444";

Get Sub Strings by length 3:

         Dim mc As MatchCollection =  Regex.Matches(str,"\w{3}")
          Dim i As Integer
          For  i = 0 To  mc.Count- 1  Step  i + 1
              Console.WriteLine("--"+mc(i).Value+"--")
          Next

output:

--111--
--122--
--223--
--333--
--444--

note: It won't return last digit i.e 4 in this case . str has 16 chars, it's returning only 15 chars,remaining string length is less than 3.

in This example Gets remaining Strings also

if Regular Expression is

\w{1,3}
output:
--111--
--122--
--223--
--333--
--444--
--4--

Example 2: Split Strings by Length using Regular Expressions.

Note: It includes chars & white spaces of length 1 to 3(max).

Dim str As String = "a ab abc abcd abcd abcdcd"
Dim mc As MatchCollection = Regex.Matches(str,"[\w\s]{1,3}")
Dim i As Integer
For  i = 0 To  mc.Count- 1  Step  i + 1
Console.WriteLine("--?" + mc(i).Value + "--"+i)
Next
 
output:
--?a a--0
--?b a--1
--?bc --2
--?abc--3
--?d a--4
--?bcd--5
--? ab--6
--?cdc--7
--?d--8
--?4--5
 

Tags:VB.NET split strings by length,How to Split a String by Length,How to split a string into smaller chunks,VB.NET splitting string in to array,Split a String by length using regular expressions, Split Strings by Regular Expressions, System.Text.RegularExpressions;,String.Substring

c# split strings by length

c# split strings by length

String methods are available to split strings by delimiters.  If user wants to split by length and last remaining portion of the string may may not be of the same length as user specified.

for ex: String str = "a ab abc abcd abcd abcdcd";

Split by 3    -> last string will be   "d " only(1char)


Here is  a method to Split a String by Length.


  static void SplitStringsByLength(int len)
      {
          Console.WriteLine("SplitStringsByLength");
          String str = "a ab abc abcd abcd abcdcd";
         
          while (!String.IsNullOrEmpty(str))
          {

              try
              {
                  Console.WriteLine(str.Substring(0, len));
                  str = str.Remove(0, len);
              }
              catch (Exception ex)
              {
                  Console.WriteLine("--"+str+"--"); break;
              }
          }
      }

call:  SplitStringsByLength(3);

OUTPUT

-a a-
-b a-
-bc -
-abc-
-d a-
-bcd-
- ab-
-cdc-
--d--



Note: String substr may through exception if string length is less than the length user specified,in that case get remaining portion of the string and break the loop.


Split a String by length using Regular Expression

User can split a string by using regular expressions.

suppose user's input:  str = "1111222233334444";

Get Sub Strings by length 3:

         MatchCollection mc= Regex.Matches(str, @"\w{3}");
          for(int i=0; i < mc.Count; i++)
          {
              Console.WriteLine("--"+mc[i].Value+"--");
        }

output:

--111--
--122--
--223--
--333--
--444--

note: It won't return last digit i.e 4 in this case . str has 16 chars, it's returning only 15 chars,remaining string length is less than 3.

in This example Gets remaining Strings also

if Regular Expression is

\w{1,3}

output:
--111--
--122--
--223--
--333--
--444--
--4--

Example 2: Split Strings by Length using Regular Expressions.


Note: It includes chars & white spaces of length 1 to 3(max).

str="a ab abc abcd abcd abcdcd" ;

MatchCollection mc=Regex.Matches(str, @"[\w\s]{1,3}");
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine("--?" + mc[i].Value + "--"+i);
}

output:
--?a a--0
--?b a--1
--?bc --2
--?abc--3
--?d a--4
--?bcd--5
--? ab--6
--?cdc--7
--?d--8
--?4--5
 

Tags:c# split strings by length,How to Split a String by Length,How to split a string into smaller chunks,C# splitting string in to array,Split a String by length using regular expressions, Split Strings by Regular Expressions, System.Text.RegularExpressions;,String.Substring

Saturday, 2 February 2013

String Sort using LINQ orderby VB.NET


String Sort using LINQ VB.NET

            
Sorting a string array using LINQ. suppose LINQ array has firstname,lastname ,default sort works on firstname. If user wants to sort on LastName. then you can follow this logic.
 name contains format:  FirstName,LastName
  Dim strFirstLastNames() As String =  New String() {"Amar,josuva","peter,hans","tom,cruise","Preeti,Zinta"};

Private  Sub SortByFirstName()
            var query = from name in strFirstLastNames
                        orderby name
                        Dim name As select

            Dim str As String
            For Each str In query
                Console.WriteLine(str)
            Next
End Sub

OUTPUT(alphabetical order)
--------------------

Amar,josuva
peter,hans
Preeti,Zinta
tom,cruise


Private  Sub SortByLastName()
            Console.WriteLine()

            var query = from name in strFirstLastNames
                        Dim lastname As let =  name.Split(New Char()
                        {
                            ","c
                        }
)(1)
                        orderby lastname
                        Dim name As select
               'For Descending  orderby lastname descending
            Dim str As String
            For Each str In query
                Console.WriteLine(str)
            Next

  End Sub
 
 OUTPUT(last name, cruise, hans ,josuva,Zinta in sorted order).
----------------------

tom,cruise
peter,hans
Amar,josuva
Preeti,Zinta


Tags:  String Sort using LINQ orderby VB.NET,LINQ LET CLAUSE,LINQ ORDERBY clause,LINQ ORDER BY keyword, String Sort using LINQ, String split in LET clause, String sorting in descending order, Order Strings in VB.NET,Arrange Strings in Ascending/descending order.