Month: October 2013

String Comparison

Posted on Updated on

Comparisons
• Whenever the user read string or data from the command prompt it needs to be tested and compared for things like blank values, capital letters, extra spaces, incorrect formats and many things. The checking of the strings is known as String Comparison.
• Strings have many in-built methods and properties that can be used for string comparisons. Some are listed below:
Static methods of string
Compare Compares the values of two strings.
Copy Create a new string from an existing string
Concat Join two or more strings in to one string
Join Concatenates two or more strings or all elements in an array
equals Compares two strings to determine whether they contain the same value. returns true if the two values are equal; otherwise returns false

Methods and properties of each instance
Trim Removes copies of specified string from the beginning and end of the string
TrimEnd Removes copies of a specified string from the end of the string
TrimStart Removes copies of a specified from the beginning of the string
Char Returns a given character at a given location
Clone Returns a copy of the stored string
Contains Checks if a string contain certain characters
CompareTo Compares the value of first string with the second string and returns a negative number if first string is less than the second, a zero if equal and a positive number if the value of first string is greater
CopyTo Copies a portion or all of the string to a new string or character array
Length Returns the number of characters stored in the sting
IndexOf Returns the index (position) of the first match for a character or a string. Returns -1 in case the character is not found
LastIndexOf Returns the location of the last match for a character or sting. Returns -1 if the values is not found
StartsWith Determines whether the value stored in a string starts with a specified character or set of the characters. Returns true if there is a match and false if not
EndsWith Determines whether the value stored in a string ends with a specified character or set of the characters. Returns true if there is a match and false if not
Insert Inserts characters into a string at specified position
PadLeft Right justifies the values of a string and then pads any remaining spaces with a specified character
PadRight left justifies the values of a string and then pads any remaining spaces with a specified character
Spilt Breaks a string into sub strings using a specified breakpoint
Remove Deletes specified number of characters from specified position in a string
Substring Returns sub string from the original string starting at a specified location. The number of characters for the substring might also be specified but is not required
ToCharArray Copies the characters in the current string to a char array
ToLower Returns a copy of the current string in to all lower case letters
ToUpper Returns a copy of the current string in to upper case letters.

static void Main(string[] args)
{
string movie1 = ” rowdy rathore “;
string movie2 = “bol@bachan”;
Console.WriteLine(“{0}”,movie1 );
Console.WriteLine(“length of movie:{0}”, movie1.Length );
movie1 = movie1.Trim();
Console.WriteLine(“length of trimmed movie:{0}”, movie1.Length );

if (movie1.Contains(“rowdy”))
{
Console.WriteLine(“rowdy is contained in {0}”, movie1);
}
else
{
Console.WriteLine(“rowdy is not in {0}”, movie1);
}

int position = movie2.IndexOf(“@”);
Console.WriteLine(“position of @ in {0} is {1}”, movie2,position );

string movie1Insert = movie1.Insert(6, “bachan”);
Console.WriteLine(“after insertion {0}”,movie1Insert );

Console.WriteLine(“after PadLeft:{0}”,movie1 .PadLeft (20,’*’));
Console.WriteLine(“after PadLeft:{0}”, movie1.PadRight (20, ‘$’));

Console.WriteLine(“after removing:{0}”, movie1.Remove (5,3));

Console.WriteLine(“after replace:{0}”, movie1.Replace (“rowdy”,”strong”));

Console.WriteLine(“after sub string:{0}”, movie1.Substring (5,3));

Console.Read();
}
}

Some more example-
static void Main(string[] args)
{
string weekdays = “jan feb mar apr may june july aug”;
string[] arrweek = weekdays.Split(‘ ‘);
Console.WriteLine(“week days are:”);
foreach(string i in arrweek )
{
Console.WriteLine(“{0}”,i );
}
string showText = string.Join(“-“,arrweek );
Console.WriteLine(“joined text:{0}”,showText );
Console.Read();
}

Advertisements

ArrayList

Posted on

ArrayList:

  • The ArrayList is one of the important classes in the System.Collection namespace.  ArrayList is a dynamic array; it will increase the size of the storage location as required. It stores the value as object. The allocation of the ArrayList can be achieved through the TrimToSize property.
  • In array you can store data only of same data type. For ex-if array has int then char cannot be stored. But this is possible with collection known as hash table.
  • Collections allow easy sorting, deleting and adding of items.
  • Array list work similar to an array.
  • ArrayList has many predefined methods listed below-
Add It will add the element as object in the ArrayList
Clear It will clear the all objects in the ArrayList
Insert It will insert the element in the specified location of the index in the ArrayList.
InsertRange It will insert the elements in the object as individual objects in the specified location.
Remove It will remove the given object in the first occurrence in the ArrayList.
RemoveAt It will remove the object as specified in the argument.
RemoveRange It will remove the set of objects in the ArrayList from the range specified.
Sort It will do the sorting of the elements in the ascending order.
Reverse It will arrange the elements in the reverse order in the ArrayList.
Contains It checks whether the objects exists or not.

 static void Main(string[] args)

{   ArrayList al = new ArrayList();

//adding item to arraylist

al.Add(“ramesh”);

al.Add(“suresh”);

al.Add(“sita”);

al.Add(“gita”);

Console.WriteLine(“add the names to the list”);

foreach (string a in al)

{

Console.WriteLine(“{0}”,a );

}

//sorting item to arraylist

al.Sort();

Console.WriteLine(“sorting names in the list”);

foreach (string a in al)

{

Console.WriteLine(“{0}”, a);

}

//removing item from arraylist

Console.WriteLine(“removing names from the list”);

al.Remove(“ramesh”);

//showing item from arraylist

foreach (string a in al)

{

Console.WriteLine(“{0}”, a);

}

Console.Read();       }    }

 

OUTPUT –

add the names to the list

ramesh

suresh

sita

gita

sorting names in the list

gita

ramesh

sita

suresh

removing names from the list

gita

sita

suresh

 

 

Display Date using Datagram Socket

Posted on

import java.net.*;

import java.util.*;

 

public class ServerTestUDP

{

DatagramSocket dataSocket;

DatagramPacket dataPacket;

InetAddress addr;

 

public static void main(String args[]) throws IOException

{

ServerTestUDP  UDPServer=new ServerTestUDP();

UDPServer.go();

}

public ServerTestUDP() throws IOException

{

addr=InetAddress.getByName(“localhost”);

dataSocket=new DatagramSocket();

}

public void go() throws Exception

{

byte[] buff;

for(;;)

{

Thread.sleep(1000);

System.out.println(“sending”);

String str=(new Date()).toString();

buff=str.getBytes();

dataPacket=new DatagramPacket(buff,buff.length,addr,1313);

dataSocket.send(dataPacket);

}

}

}

 

public class ClientTestUDP

{

DatagramSocket dataSocket;

DatagramPacket dataPacket;

Public static void main(String args[])

{

ClientTestUDP UDPCleint=new ClientTestUDP();

UDPClient.go();

}

public void go()

{

byte B[]=new byte[64];

try

{

dataSocket =new DatagramSocket(1313);

dataPacket=new DatagramPacket(B,B.length);

while(true)

{

dataSocket.received(dataPacket);

str=new String(dataPacket.getData());

System.out.println(“Time Signal received from “+dataPacket.getAddress()+”\n times is :”+str);

}

}catch(Exception excep){ System.out.println(“exception raised”);}

}

}

 

 

The Default Value Types

Posted on Updated on

The Default Value type

Class/Structure Name

Data Type in C#.NET 

NO. of Bytes 

 Range

 Integer

 System.Byte

 Byte

 1(Unsigned)

 0 to 255

 

 System.SByte

 Sbyte

 1(Signed)

 -128 to 127

 

 System.Int16

 Short

 2(Signed)

 -32,768 to 32,767

 

 System.Uint16

 Ushort

 2(Unsigned)

 0 to 65,535

 

 System.Int32

 Int

 4(Signed)

 -2,147,483,648 to 2,147,483,647

 

 System.Uint32

 Uint

 4(Unsigned)

 0 to 4,294,967,295

 

 System.Int64

 Long

 8(Signed)

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

 

 System.Uint64

 Ulong

 8(Unsigned)

 0 to 18,446,744,073,709,551,615

 Float

 

 

 

 

 

 Single

 Float

 4

 ±1.5e−45 to ±3.4e38
(Precision:7 digits)

 

 Double

 Double

 8

 ±5.0e−324 to ±1.7e308
(Precision:15-16 digits)

 

 Decimal

 Decimal

 16

 (-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
(Precision:28-29 digits)

 Character

 

 

 

 

 

 Char

 Char

 2

 0 to 65535

 

 String

 String

 size varies

 Alphabets, numbers & special characters

 Other

 

 

 

 

 

 DateTime

 DateTime

 8

 

 

 Boolean

 Bool

 1

 

 

 Object

 Object

 size varies

 Can store any type of value

Review of .NET framework

Posted on Updated on

Review of .NET framework

• .NET Framework is an internal windows component that supports the execution of application created by using various programming languages, such as visual C#, visual basic, visual F#, and visual C++.
• These programming language environments are a part of visual studio .net. The .net framework consists of a virtual execution system called the common language runtime (CLR) and a set of class libraries.
• CLR is a Microsoft product of the common language infrastructure (CLI), which is a standard of creating application in a variety of high – level programming languages and executing these applications in different system environments.
• Microsoft released the first version of .NET with the name .NET framework 1.0 on February 13, 2002, along with the visual studio. Net integrated Development Environment (IDE). The second revised version of .NET took nearly a year to release and was known as .net Framework 1.1.
• Microsoft visual studio.NET, BETTER KNOWN AS VISUAL STUDIO .NET 2003, was also a part of the second release. The next version, .NET framework 2.0 was released with visual studio 2005 on November 07, 2005. NET framework 3.0, formerly called WinFX, was then released on November 06, 2006. The version, NET framework 3.5 was released with visual studio 2008 on November 19, 2007.
• The latest version, the .NET framework 4.0 was announced by Microsoft on September 29, 2008 and on April 12.2010, the final version of .net framework 4.0 was released.
• The new version of .NET provides a framework that helps in designing portable, scalable, and robust applications. The applications developed in .NET framework 4.0 can be executed in a distributed environment as the framework provides a computing platform in a managed environment. The main components of .NET Framework 4.0 are given as follows:

1) CLR
2) Common Type System (CTS)
3) Metadata
4) Assembly
5) .NET framework class library
6) Windows forms
7) ASP.NET and ASP.NET AJAX
8) ADO.NET

Created by Chanda Singh

Difference between AWT and Swing Components

Posted on Updated on

created by Ashwin Mehta and Sarika Shah

Sr. no. AWT SWING
1. Heavy weight Light weight
2. Native component Pure java Component
3. Native look and feel Better look and feel
4. Does not have complex component Has additional components like JTree, JTable, JProgressBar, JSlider, etc
5. Applet can not have menu JApplet can have menu.
6. List has scrollbar JList does not support scrolling but this can be done using scrollPane
7. Components can be added directly on the Frame or window While adding components on Frame or window, they have to be added on its conetent pane.
8. Does not have slidePane or TabbedPane Has slidePane or TabbedPane
9. Does not support MDI window MDI can be achived using InternalFrame Object