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

来源:互联网 发布:adobe pdf for mac 编辑:程序博客网 时间:2024/06/06 02:07

1.定义字符串引用变量

     string name;

     name="Heailey";

    引用变量的规则:

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

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

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

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

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

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

2使用字符串变量

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

字符串连接:

[csharp] view plaincopy
  1. <pre name="code" class="csharp">string name="Hailey";  
  2. name=name+"Mohr";  


字符串操作:

[csharp] view plaincopy
  1. txtLength.Text=txtInput.Text.Length.ToString();  

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

如:

[csharp] view plaincopy
  1. int length;  
  2. string zip="80122";  
  3. length=zip.Length;

属性和方法的重要区别:

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

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

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

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

[csharp] view plaincopy
  1. <pre name="code" class="csharp"><pre name="code" class="csharp">txtLength.Text=txtInput.Text.Length.ToString();  

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

[csharp] view plaincopy
  1. txtToUpper.Text=txtInput.Text.ToUpper();  
[csharp] view plaincopy
  1. txtToLower.Text=txtInput.Text.ToLower();  

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

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

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

[csharp] view plaincopy
  1. index = textBox1.Text.IndexOf(textBox5.Text, 0);  
  2. labelStingtotest.Text = "textBox1.Text.IndexOf(\"" + textBox12.Text + "\",0)=";  
  3. textBox12.Text=index.ToString ();  
IndexOf()需要两个参数,一个是要查找的字符,另一个是起始位置。

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

[csharp] view plaincopy
  1. index = textBox1.Text.LastIndexOf(textBox6.Text);  
  2. labelStingtotest.Text = "textBox1.Text.LastIndexOf(\"" + textBox13.Text + "\")=";  
  3. textBox13.Text = index.ToString();  

 (4)搜索字符串

使用Substring()f方法

[csharp] view plaincopy
  1. labeltxtInputTextSubstring.Text ="textBox1.Text.Substring("+start .ToString ()+","+howMany .ToString ()+")=";  
  2. textBox15.Text =textBox1.Text.Substring(start,howMany);  
TryParse()方法是将用户在两个文本框对象中输入的数字字符转换成名为start和howMany的整数变量。start是开始位置,howMany是长度。

(5)删除字符串

[csharp] view plaincopy
  1. temp=textBox1.Text;  
  2. index=temp.IndexOf(textBox8.Text);  
  3. if (index > 0)  
  4. {  
  5.      textBox9.Text = temp.Remove(index, textBox8.Text.Length);  
  6. }  
先用indexOf()方法获得“目标字符串”所在的位置。在Remove中,第一个参数是字符串的起始位置,第二个参数是字符串的长度。

(6)替换字符串

[csharp] view plaincopy
  1. temp=textBox1.Text;  
  2. textBox11.Text =temp.Replace (textBox10.Text ,textBox16.Text);  
Replace()方法需要两个参数。第一个参数是希望替换的字符串,第二个参数是希望取代第一个参数的字符串。
[csharp] view plaincopy
  1. textBox11.Text =temp.Replace (“1234567890”,“***”);  

完整代码示例:


[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace String_Tester  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void btnText_Click(object sender, EventArgs e)  
  20.         {  
  21.             bool flag;  
  22.             int index;  
  23.             int start;  
  24.             int howMany;  
  25.             string temp;  
  26.             labelStingtotest.Text = "";  
  27.   
  28.             textBox2.Text = textBox1.Text.Length.ToString();  
  29.             textBox3.Text = textBox1.Text.ToUpper();  
  30.             textBox4.Text = textBox1.Text.ToLower();  
  31.   
  32.             //Index of  
  33.             index = textBox1.Text.IndexOf(textBox5.Text, 0);  
  34.             labelStingtotest.Text = "textBox1.Text.IndexOf(\"" + textBox12.Text + "\",0)=";  
  35.             textBox12.Text=index.ToString ();  
  36.   
  37.             //LastIndexOF  
  38.             index = textBox1.Text.LastIndexOf(textBox6.Text);  
  39.             labelStingtotest.Text = "textBox1.Text.LastIndexOf(\"" + textBox13.Text + "\")=";  
  40.             textBox13.Text = index.ToString();  
  41.               
  42.             //Substring  
  43.             flag=int.TryParse(textBox7.Text ,out start);  
  44.             if(flag==false)  
  45.             {  
  46.                 MessageBox .Show ("Improper numeric input.Re-enter.");  
  47.                 textBox7.Focus();  
  48.                 return ;  
  49.             }  
  50.   
  51.              flag=int.TryParse(textBox14.Text ,out howMany);  
  52.             if(flag==false)  
  53.             {  
  54.                 MessageBox .Show ("Improper numeric input.Re-enter.");  
  55.                 textBox14.Focus();  
  56.                 return ;  
  57.             }  
  58.   
  59.             labeltxtInputTextSubstring.Text ="textBox1.Text.Substring("+start .ToString ()+","+howMany .ToString ()+")=";  
  60.             textBox15.Text =textBox1.Text.Substring(start,howMany);  
  61.               
  62.             //Remove  
  63.             temp=textBox1.Text;  
  64.             index=temp.IndexOf(textBox8.Text);  
  65.             if (index > 0)  
  66.             {  
  67.                 textBox9.Text = temp.Remove(index, textBox8.Text.Length);  
  68.             }  
  69.   
  70.               
  71.             //Replace  
  72.             temp=textBox1.Text;  
  73.             textBox11.Text =temp.Replace (textBox10.Text ,textBox16.Text);  
  74.                
  75.   
  76.         }  
  77.   
  78.         private void btnClose_Click(object sender, EventArgs e)  
  79.         {  
  80.             Close();  
  81.         }  
  82.   
  83.           
  84.     }  
  85. }  


0 0
原创粉丝点击