Search This Blog

Apr 22, 2014

c# - Convert comma separated string to array

Split separates strings. Often strings have delimiter characters in their data. Split handles splitting upon string and character delimiters.Methods can be combined. Using IndexOf and Substring together is another way to split strings.
Use Split to separate parts from a string. If your input is "A B C", split on the space to get an array of "A", "B" and "C".

string str = "1,2,3,4";

        // 1st way
        string[] strArray = str.Split(new char[] { ',' });
        int[] intArray = new int[strArray.Length];
        for (int i = 0; i < strArray.Length; i++)
            intArray[i] = int.Parse(strArray[i]);

        // 2nd way
        string[] array = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

Apr 5, 2014

contextual help dialogs using HelpProvider Class

The HelpProvider control offers contextual help dialogs. Sometimes a user wants a verbose description of a control. With HelpProvider we provide this description by adding a Help button and setting some properties.

using System;
using System.Drawing;
using System.Windows.Forms;

How to extract a portion of a string variable in C#

There are many ways to do this, i am exaplaining two of them.

1. String.Remove Method (Int32, Int32)

Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

Syntax :

public string Remove(
    int startIndex,
    int count
example :

My string is "C:\Program Files\Microsoft Visual Studio\VSS\users" and i want to

"C:\Program Files\Microsoft Visual Studio\VSS" then

    string s = "svn://mcdssrv/repos/currecnt/class/";
2. String.Substring Method

Retrieves a substring from this instance.

Substring takes the start index (zero-based) and the number of characters you want to copy.

example :

string str = "C:\Program Files\Microsoft Visual Studio\VSS\users";
    str = str.Substring(0, str.LastIndexOf('\') + 1);

3. By using both method

    var subString = "C:\Program Files\Microsoft Visual Studio\VSS\users";
        subString = subString.Remove(subString.LastIndexOf('\')+1);
Related Posts Plugin for WordPress, Blogger...