您的位置:首页 > 其它

引用数据类型---字符串变量

2014-07-31 19:24 411 查看
1.定义字符串引用变量

     string name;

     name="Heailey";

    引用变量的规则:

     (1)引用变量的rvalue只能有两种值:null和内存地址。

     (2)如果引用变量的rvalue是null,引用变量就不含有有用的数据。

     (3)如果引用变量的rvalue不是null.它就包含存储与引用变量关联的数据的地址。

    引用类型变量和值类型变量的区别:

    结论很简单:有rvalue的值类型是数据。引用类型有一个rvalue。要么引用一个数据(内存地址),要么不引用任何数据(null)。

    所以,传递实际内存位置的方法称为按引用传递。

2使用字符串变量

字符串变量是允许存储和操作文本数据的引用变量。

字符串连接:

[csharp] view
plaincopy

<pre name="code" class="csharp">string name="Hailey";  

name=name+"Mohr";  



字符串操作:

[csharp] view
plaincopy

txtLength.Text=txtInput.Text.Length.ToString();  

字符串变量实际上是一个封装对象,它可以用点运算符打开。然后,通过点运算符可以使用该对象的属性和方法。

如:

[csharp] view
plaincopy

int length;  

string zip="80122";  

length=zip.Length;

属性和方法的重要区别:

记住:属性是与对象关联的变量,因此,它们不要求在名称后面有()。另一方面,所有方法必须再在方法名后面有();

实例:使用字符串类方法和属性

(1)字符串的长度:length属性

length属性存储字符串的长度。

[csharp] view
plaincopy

<pre name="code" class="csharp"><pre name="code" class="csharp">txtLength.Text=txtInput.Text.Length.ToString();  




(2)修改字符串的大小写:ToUpper()和ToLower()

[csharp] view
plaincopy

txtToUpper.Text=txtInput.Text.ToUpper();  

[csharp] view
plaincopy

txtToLower.Text=txtInput.Text.ToLower();  

(3)查找特定的字符串:IndexOf()

假设需要在一个字符串中查找特定的字符。输入字符v并用IndexOf()方法查找该字符第一次出现的位置。

编程语言在字符串中计算字符数量时将第一个位置称为位置0,而不是位置1.

[csharp] view
plaincopy

index = textBox1.Text.IndexOf(textBox5.Text, 0);  

labelStingtotest.Text = "textBox1.Text.IndexOf(\"" + textBox12.Text + "\",0)=";  

textBox12.Text=index.ToString ();  

IndexOf()需要两个参数,一个是要查找的字符,另一个是起始位置。

  (3)搜索字符的最后一次出现位置:LastIndexOf()

[csharp] view
plaincopy

index = textBox1.Text.LastIndexOf(textBox6.Text);  

labelStingtotest.Text = "textBox1.Text.LastIndexOf(\"" + textBox13.Text + "\")=";  

textBox13.Text = index.ToString();  

 (4)搜索字符串

使用Substring()f方法

[csharp] view
plaincopy

labeltxtInputTextSubstring.Text ="textBox1.Text.Substring("+start .ToString ()+","+howMany .ToString ()+")=";  

textBox15.Text =textBox1.Text.Substring(start,howMany);  

TryParse()方法是将用户在两个文本框对象中输入的数字字符转换成名为start和howMany的整数变量。start是开始位置,howMany是长度。

(5)删除字符串

[csharp] view
plaincopy

temp=textBox1.Text;  

index=temp.IndexOf(textBox8.Text);  

if (index > 0)  

{  

     textBox9.Text = temp.Remove(index, textBox8.Text.Length);  

}  

先用indexOf()方法获得“目标字符串”所在的位置。在Remove中,第一个参数是字符串的起始位置,第二个参数是字符串的长度。

(6)替换字符串

[csharp] view
plaincopy

temp=textBox1.Text;  

textBox11.Text =temp.Replace (textBox10.Text ,textBox16.Text);  

Replace()方法需要两个参数。第一个参数是希望替换的字符串,第二个参数是希望取代第一个参数的字符串。

[csharp] view
plaincopy

textBox11.Text =temp.Replace (“1234567890”,“***”);  

完整代码示例:

[csharp] view
plaincopy

using System;  

using System.Collections.Generic;  

using System.ComponentModel;  

using System.Data;  

using System.Drawing;  

using System.Linq;  

using System.Text;  

using System.Windows.Forms;  

  

namespace String_Tester  

{  

    public partial class Form1 : Form  

    {  

        public Form1()  

        {  

            InitializeComponent();  

        }  

  

        private void btnText_Click(object sender, EventArgs e)  

        {  

            bool flag;  

            int index;  

            int start;  

            int howMany;  

            string temp;  

            labelStingtotest.Text = "";  

  

            textBox2.Text = textBox1.Text.Length.ToString();  

            textBox3.Text = textBox1.Text.ToUpper();  

            textBox4.Text = textBox1.Text.ToLower();  

  

            //Index of  

            index = textBox1.Text.IndexOf(textBox5.Text, 0);  

            labelStingtotest.Text = "textBox1.Text.IndexOf(\"" + textBox12.Text + "\",0)=";  

            textBox12.Text=index.ToString ();  

  

            //LastIndexOF  

            index = textBox1.Text.LastIndexOf(textBox6.Text);  

            labelStingtotest.Text = "textBox1.Text.LastIndexOf(\"" + textBox13.Text + "\")=";  

            textBox13.Text = index.ToString();  

              

            //Substring  

            flag=int.TryParse(textBox7.Text ,out start);  

            if(flag==false)  

            {  

                MessageBox .Show ("Improper numeric input.Re-enter.");  

                textBox7.Focus();  

                return ;  

            }  

  

             flag=int.TryParse(textBox14.Text ,out howMany);  

            if(flag==false)  

            {  

                MessageBox .Show ("Improper numeric input.Re-enter.");  

                textBox14.Focus();  

                return ;  

            }  

  

            labeltxtInputTextSubstring.Text ="textBox1.Text.Substring("+start .ToString ()+","+howMany .ToString ()+")=";  

            textBox15.Text =textBox1.Text.Substring(start,howMany);  

              

            //Remove  

            temp=textBox1.Text;  

            index=temp.IndexOf(textBox8.Text);  

            if (index > 0)  

            {  

                textBox9.Text = temp.Remove(index, textBox8.Text.Length);  

            }  

  

              

            //Replace  

            temp=textBox1.Text;  

            textBox11.Text =temp.Replace (textBox10.Text ,textBox16.Text);  

               

  

        }  

  

        private void btnClose_Click(object sender, EventArgs e)  

        {  

            Close();  

        }  

  

          

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐