Showing posts with label string operations. Show all posts
Showing posts with label string operations. Show all posts

Monday, February 27, 2017

How to use C# string Format

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double dNum = 0;
            dNum = 32.123456789;
            MessageBox.Show("Formated String " + string.Format("{0:n4}", dNum)); 
        }
    }
}


When you run this C# source code you will get "Formated String 32.1234" 

How to use C# string Length

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            str = "This is a Test";
            MessageBox.Show(str.Length.ToString()); 
        }
    }
}


When you execute this C# source code you will get 14 in the messagebox. 

How to use C# string Clone

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
                string str = "Clone() Test";
                string clonedString = null;
                clonedString = (String)str.Clone();
                MessageBox.Show (clonedString);
            }
        }
    }
}


When you run this C# program you will get the same content of the 
first string "Clone() Test" 

How to use C# string Concat

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str1 = null;
            string str2 = null;

            str1 = "Concat() ";
            str2 = "Test";
            MessageBox.Show(string.Concat(str1, str2)); 
        }
    }
}


When you run this C# source code you will get "Concat() Test " in message box. 

How to use C# string Split

using System; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = null; string[] strArr = null; int count = 0; str = "CSharp split test"; char[] splitchar = { ' ' }; strArr = str.Split(splitchar); for (count = 0; count <= strArr.Length - 1; count++) { MessageBox.Show(strArr[count]); } } } }