NEWS
Showing posts with label Code Snippets. Show all posts
Showing posts with label Code Snippets. Show all posts

Wednesday, July 6, 2011

Remove characters from a Numeric String

facing issues to know how do we remove characters from a String?


Consider the following example for mobile number cleanup, Take a textbox on the form

Rename it to ="TxtMobile"

Add a label and rename it to"LblResult"

Add a label and rename it to "CmdSubmit"


Declare a public string

Public Mobile As String

Under "CmdSubmit" click write the following code:


Protected Sub CmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdSubmit.Click


If Not TxtMobile.Text = "" Then


LblResult.Text = ""


Mobile = TxtMobile.Text


CheckforChars()


Lblresult.Text = Mobile


Else


Label1.Text = "Enter Mobile Number with some characters"


End If

End SubThe above code assigns textbox value to public string we have declare above and passes it to sub CheckforChars(), Then the results are shown in Lblrsult.

Aslo write two small functions to remove characters from string of numeric



The below functions removes all the digits or numeric values from a string and stores all the chacters


Public Shared Function RemoveDigits(ByVal key As String) As String


Return Regex.Replace(key, "\d", "")


End Function

The Mobile number with characters is passed through the above function in the below sub and returned value from the above function is stored in a string called Value1

The characters present in Mobile number are replaced with nothing, you can also use .remove here instead of replace

Private Sub CheckforChars()


Dim value1 As String = RemoveDigits(Mobile)


Mobile = Mobile.Replace(value1, "")


End Sub

Wednesday, June 29, 2011

Load Text from File to String [C#]

To load text from file to string you can use StreamReader.Re­adToEnd method. First create new instance of StreamReader. As a parameter in constructor you can pass string with file path or Stream instance. Default encoding is UTF-8.


using System.IO;




StreamReader streamReader = new StreamReader(filePath);

string text = streamReader.ReadToEnd();

streamReader.Close();





Tuesday, May 24, 2011

Stream to ByteArray and Distinct datarows

To get Stream as Byte Array in VB.Net

Private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
Dim _streamLength As Integer = Convert.ToInt32(stream.Length)
Dim _fileData As Byte() = New Byte(_streamLength) {}
' Read the file into a byte array
stream.Read(_fileData, 0, _streamLength)
stream.Close()
Return _fileData
End Function

To select distinct data rows in a datatable:

private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
object[] lastValues;
DataTable newTable;
DataRow[] orderedRows;

if (FieldNames == null || FieldNames.Length == 0)
throw new ArgumentNullException("FieldNames");

lastValues = new object[FieldNames.Length];
newTable = new DataTable();

foreach (string fieldName in FieldNames)
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));

foreach (DataRow row in orderedRows)
{
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
{
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

setLastValues(lastValues, row, FieldNames);
}
}

return newTable;
}

private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
bool areEqual = true;

for (int i = 0; i < fieldNames.Length; i++) { if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]])) { areEqual = false; break; } } return areEqual; } private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames) { foreach (string field in fieldNames) newRow[field] = sourceRow[field]; return newRow; } private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames) { for (int i = 0; i < fieldNames.Length; i++) lastValues[i] = sourceRow[fieldNames[i]]; }

Code to remove duplicates in datatable

Public Function RemoveDuplicateRows(ByVal _dTable As DataTable, ByVal _colName As String) As DataTable
Dim _hTable As New Hashtable()
Dim _duplicateList As New ArrayList()

For Each drow__1 As DataRow In _dTable.Rows
If _hTable.Contains(drow__1(_colName)) Then
_duplicateList.Add(drow__1)
Else
_hTable.Add(drow__1(_colName), String.Empty)
End If
Next

For Each dRow__2 As DataRow In _duplicateList
_dTable.Rows.Remove(dRow__2)
Next

Return _dTable
End Function