C#成神之路<8> C#引用数据类型详述
2016-02-03 21:06
190 查看
1、字符串变量
属性:用于存储文本数据。(比如文本框对象中的Text属性实际上就是字符串变量,TryParse方法就是把字符串数据转换成数值数据类型。)
(1)定义字符串引用变量
VS编译器处理字符串变量的方法和之前的数值数据类型存储方式相同。唯一不同是,其rvalue的值如果程序中没有赋值,那么系统默认为“null”。
注:C#语言默认使用Unicode字符集。
(2)引用类型不同于值类型的原因
I.在必要时,引用变量可以与不同大小的数据关联。
II.固定字节数非常适合于值数据类型,但是并不适用于字符串数据。
(3)引用变量规则
I.引用变量的rvalue只能有两种值:null和内存地址。
II.如果引用变量的rvalue为null,引用变量就不包含有用的数据。
III.如果引用变量的rvalue不是null,他就包含存储与引用变量相关联的数据的地址。
和值类型不同,字符串数据中的rvalue不存放在程序中使用的实际数据的引用变量。因为引用的rvalue的值只能存放null和内存地址,所以引用变量的大小总是4字节,这意味着引用变量的bucket的大小总是相同。
(4)C#传值传引用的比较
按引用传递:
I.省时省力,节省内存。
II.使用引用变量的方法具有直接访问数据的权限
III.传递实际内存位置的方式。(直接访问原始数据)
IV.代价:数据有污染的可能性。
按值传递:
I.使用值类型方法接受用户的值是值的副本而不是值的本身。这种方法不能够永远的改变用户的值。
II.内存中有两个桶:原始桶和包含副本的桶。(保护了原始数据,更改的只是副值)
(5)提高效率
一行代码能够完成的任务,尽量用一行代码完成。代码量越少,只要完成相同任务,就可以提升程序速度。
同时也是为了减少输入量。
(6)使用字符串变量
字符串链接:+
简化赋值:+=(只有这种简化版的赋值运算符对字符串有用。)
(7)字符串操作
.Length : 返回字符串对象的length属性。
注:属性是与对象关联的变量,因此,他们不要求在对象后面有圆括号。
而方法后面必须有圆括号。
以上是对字符串的诸多应用,下面对出现的诸多方法进行详述:
(1)数据包装器
在非OOP语言中,值类型和引用类型不能很好的融合在一起。另一方面C#从开始就被设计成面向对象编程语言。C#提供了风装置类型的包装器类。值包装器认为有一个属性(它的值)以及六个方法。
(2)修改字符串的大小写
ToUpper 和ToLower。
(3)查找特定的字符
IndexOf()
假设需要在一个字符串中查找特定的字符。(计算字符串数量的时候把第一个位置称为位置0)
indexof的两个参数值为:希望查找的字符串(txtsearchchar.text)和希望从哪个索引位置开始索引。
如果找不到与传递给他的字符序列完全匹配的字符,则他会返回-1。
LastIndexOf()搜索字符串的特定字符的最后一次出现的位置。
只有一个参数,就是他要搜索的字符。如果没有找到匹配字符,则返回 -1。
(4)搜索子字符串
以上的代码,从字符串中的第start+1个字符开始搜索子字符串,并复制howmany个字符。
(5)删除子字符串
Remove的参数:第一个是字符串中目标子字符串的位置,利用length属性得到要删除字符的个数。使程序更加灵活。
(6)删除子字符串
第一行的复制输入字符串,从而不会永久地修改txtinput文本框中的text属性。
Rplace的两个参数:第一个参数是希望替换的字符串,第二个参数是希望取代第一个参数的字符串。如果 原始字符串中不存在目标字符串,那么就不会修改原始字符串。
这里写代码片
属性:用于存储文本数据。(比如文本框对象中的Text属性实际上就是字符串变量,TryParse方法就是把字符串数据转换成数值数据类型。)
(1)定义字符串引用变量
VS编译器处理字符串变量的方法和之前的数值数据类型存储方式相同。唯一不同是,其rvalue的值如果程序中没有赋值,那么系统默认为“null”。
注:C#语言默认使用Unicode字符集。
(2)引用类型不同于值类型的原因
I.在必要时,引用变量可以与不同大小的数据关联。
II.固定字节数非常适合于值数据类型,但是并不适用于字符串数据。
(3)引用变量规则
I.引用变量的rvalue只能有两种值:null和内存地址。
II.如果引用变量的rvalue为null,引用变量就不包含有用的数据。
III.如果引用变量的rvalue不是null,他就包含存储与引用变量相关联的数据的地址。
和值类型不同,字符串数据中的rvalue不存放在程序中使用的实际数据的引用变量。因为引用的rvalue的值只能存放null和内存地址,所以引用变量的大小总是4字节,这意味着引用变量的bucket的大小总是相同。
(4)C#传值传引用的比较
按引用传递:
I.省时省力,节省内存。
II.使用引用变量的方法具有直接访问数据的权限
III.传递实际内存位置的方式。(直接访问原始数据)
IV.代价:数据有污染的可能性。
按值传递:
I.使用值类型方法接受用户的值是值的副本而不是值的本身。这种方法不能够永远的改变用户的值。
II.内存中有两个桶:原始桶和包含副本的桶。(保护了原始数据,更改的只是副值)
(5)提高效率
一行代码能够完成的任务,尽量用一行代码完成。代码量越少,只要完成相同任务,就可以提升程序速度。
同时也是为了减少输入量。
(6)使用字符串变量
字符串链接:+
简化赋值:+=(只有这种简化版的赋值运算符对字符串有用。)
(7)字符串操作
.Length : 返回字符串对象的length属性。
注:属性是与对象关联的变量,因此,他们不要求在对象后面有圆括号。
而方法后面必须有圆括号。
using System; using System.Windows.Forms; public class frmMain : Form { private Label label2; private Label label3; private Label label4; private Label label5; private Label label6; private Label label7; private Label label8; private Label label9; private Label label10; private Label label11; private Label label12; private Button btnTest_Click; private TextBox txtInput; private TextBox txtLength; private TextBox txtToUpper; private TextBox txtToLower; private TextBox txtSearchChar; private TextBox txtSearchIndex; private TextBox txtLastChar; private TextBox txtLastIndexOf; private TextBox txtStartIndex; private TextBox txtEndIndex; private Label lblSubstring; private TextBox txtSubstringResault; private TextBox txtRemove; private TextBox txtRemoveResault; private TextBox txtReplaceChars; private TextBox txtReplaceWith; private TextBox txtReplaceResault; private Button btnExit; private Label lblIndexOf; private Label lblLastIndexOf; private Label label1; #region Windows code private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); this.label10 = new System.Windows.Forms.Label(); this.label11 = new System.Windows.Forms.Label(); this.label12 = new System.Windows.Forms.Label(); this.btnTest_Click = new System.Windows.Forms.Button(); this.txtInput = new System.Windows.Forms.TextBox(); this.txtLength = new System.Windows.Forms.TextBox(); this.txtToUpper = new System.Windows.Forms.TextBox(); this.txtToLower = new System.Windows.Forms.TextBox(); this.txtSearchChar = new System.Windows.Forms.TextBox(); this.txtSearchIndex = new System.Windows.Forms.TextBox(); this.txtLastChar = new System.Windows.Forms.TextBox(); this.txtLastIndexOf = new System.Windows.Forms.TextBox(); this.txtStartIndex = new System.Windows.Forms.TextBox(); this.txtEndIndex = new System.Windows.Forms.TextBox(); this.lblSubstring = new System.Windows.Forms.Label(); this.txtSubstringResault = new System.Windows.Forms.TextBox(); this.txtRemove = new System.Windows.Forms.TextBox(); this.txtRemoveResault = new System.Windows.Forms.TextBox(); this.txtReplaceChars = new System.Windows.Forms.TextBox(); this.txtReplaceWith = new System.Windows.Forms.TextBox(); this.txtReplaceResault = new System.Windows.Forms.TextBox(); this.btnExit = new System.Windows.Forms.Button(); this.lblIndexOf = new System.Windows.Forms.Label(); this.lblLastIndexOf = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label1.Location = new System.Drawing.Point(52, 42); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(211, 23); this.label1.TabIndex = 0; this.label1.Text = "String To Test"; this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight; this.label1.Click += new System.EventHandler(this.label1_Click); // // label2 // this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label2.Location = new System.Drawing.Point(52, 82); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(211, 23); this.label2.TabIndex = 1; this.label2.Text = "String Length"; this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight; this.label2.Click += new System.EventHandler(this.label2_Click); // // label3 // this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label3.Location = new System.Drawing.Point(52, 117); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(211, 23); this.label3.TabIndex = 2; this.label3.Text = "String ToUpper"; this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label4 // this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label4.Location = new System.Drawing.Point(52, 160); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(211, 23); this.label4.TabIndex = 3; this.label4.Text = "String To Lower"; this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label5 // this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label5.Location = new System.Drawing.Point(52, 202); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(211, 23); this.label5.TabIndex = 4; this.label5.Text = "Search for character"; this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label6 // this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label6.Location = new System.Drawing.Point(52, 247); this.label6.Name = " 1a410 label6"; this.label6.Size = new System.Drawing.Size(211, 23); this.label6.TabIndex = 5; this.label6.Text = "Search for last character"; this.label6.TextAlign = System.Drawing.ContentAlignment.TopRight; this.label6.Click += new System.EventHandler(this.label6_Click); // // label7 // this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label7.Location = new System.Drawing.Point(52, 289); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(211, 23); this.label7.TabIndex = 6; this.label7.Text = "Extract Substring from index"; this.label7.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label8 // this.label8.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label8.Location = new System.Drawing.Point(394, 289); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(150, 23); this.label8.TabIndex = 7; this.label8.Text = "for thi many characters"; this.label8.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label9 // this.label9.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label9.Location = new System.Drawing.Point(52, 330); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(211, 23); this.label9.TabIndex = 8; this.label9.Text = "txtInput.Remove()"; this.label9.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label10 // this.label10.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label10.Location = new System.Drawing.Point(52, 370); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(211, 23); this.label10.TabIndex = 9; this.label10.Text = "Find"; this.label10.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label11 // this.label11.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label11.Location = new System.Drawing.Point(460, 370); this.label11.Name = "label11"; this.label11.Size = new System.Drawing.Size(103, 23); this.label11.TabIndex = 10; this.label11.Text = "Replace with"; this.label11.TextAlign = System.Drawing.ContentAlignment.TopRight; // // label12 // this.label12.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label12.Location = new System.Drawing.Point(52, 407); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(211, 23); this.label12.TabIndex = 11; this.label12.Text = "txtInput.Replace()"; this.label12.TextAlign = System.Drawing.ContentAlignment.TopRight; // // btnTest_Click // this.btnTest_Click.Location = new System.Drawing.Point(52, 455); this.btnTest_Click.Name = "btnTest_Click"; this.btnTest_Click.Size = new System.Drawing.Size(105, 23); this.btnTest_Click.TabIndex = 12; this.btnTest_Click.Text = "&Test"; this.btnTest_Click.UseVisualStyleBackColor = true; this.btnTest_Click.Click += new System.EventHandler(this.btnTest_Click_Click); // // txtInput // this.txtInput.Location = new System.Drawing.Point(278, 44); this.txtInput.Name = "txtInput"; this.txtInput.Size = new System.Drawing.Size(734, 21); this.txtInput.TabIndex = 13; // // txtLength // this.txtLength.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtLength.Location = new System.Drawing.Point(278, 83); this.txtLength.Multiline = true; this.txtLength.Name = "txtLength"; this.txtLength.ReadOnly = true; this.txtLength.Size = new System.Drawing.Size(266, 21); this.txtLength.TabIndex = 14; // // txtToUpper // this.txtToUpper.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtToUpper.Location = new System.Drawing.Point(278, 118); this.txtToUpper.Multiline = true; this.txtToUpper.Name = "txtToUpper"; this.txtToUpper.ReadOnly = true; this.txtToUpper.Size = new System.Drawing.Size(734, 21); this.txtToUpper.TabIndex = 15; // // txtToLower // this.txtToLower.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtToLower.Location = new System.Drawing.Point(278, 160); this.txtToLower.Multiline = true; this.txtToLower.Name = "txtToLower"; this.txtToLower.ReadOnly = true; this.txtToLower.Size = new System.Drawing.Size(734, 21); this.txtToLower.TabIndex = 16; // // txtSearchChar // this.txtSearchChar.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtSearchChar.Location = new System.Drawing.Point(278, 202); this.txtSearchChar.Name = "txtSearchChar"; this.txtSearchChar.Size = new System.Drawing.Size(101, 21); this.txtSearchChar.TabIndex = 17; // // txtSearchIndex // this.txtSearchIndex.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtSearchIndex.Location = new System.Drawing.Point(879, 200); this.txtSearchIndex.Multiline = true; this.txtSearchIndex.Name = "txtSearchIndex"; this.txtSearchIndex.ReadOnly = true; this.txtSearchIndex.Size = new System.Drawing.Size(133, 21); this.txtSearchIndex.TabIndex = 18; // // txtLastChar // this.txtLastChar.Location = new System.Drawing.Point(278, 247); this.txtLastChar.Name = "txtLastChar"; this.txtLastChar.Size = new System.Drawing.Size(101, 21); this.txtLastChar.TabIndex = 19; // // txtLastIndexOf // this.txtLastIndexOf.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtLastIndexOf.Location = new System.Drawing.Point(879, 245); this.txtLastIndexOf.Multiline = true; this.txtLastIndexOf.Name = "txtLastIndexOf"; this.txtLastIndexOf.ReadOnly = true; this.txtLastIndexOf.Size = new System.Drawing.Size(133, 21); this.txtLastIndexOf.TabIndex = 20; // // txtStartIndex // this.txtStartIndex.Location = new System.Drawing.Point(278, 290); this.txtStartIndex.Name = "txtStartIndex"; this.txtStartIndex.Size = new System.Drawing.Size(100, 21); this.txtStartIndex.TabIndex = 21; // // txtEndIndex // this.txtEndIndex.Location = new System.Drawing.Point(551, 290); this.txtEndIndex.Name = "txtEndIndex"; this.txtEndIndex.Size = new System.Drawing.Size(94, 21); this.txtEndIndex.TabIndex = 22; // // lblSubstring // this.lblSubstring.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblSubstring.Location = new System.Drawing.Point(651, 289); this.lblSubstring.Name = "lblSubstring"; this.lblSubstring.Size = new System.Drawing.Size(222, 23); this.lblSubstring.TabIndex = 23; this.lblSubstring.Text = "txtInput.Test.Substring()"; this.lblSubstring.TextAlign = System.Drawing.ContentAlignment.TopRight; // // txtSubstringResault // this.txtSubstringResault.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtSubstringResault.Location = new System.Drawing.Point(879, 289); this.txtSubstringResault.Multiline = true; this.txtSubstringResault.Name = "txtSubstringResault"; this.txtSubstringResault.ReadOnly = true; this.txtSubstringResault.Size = new System.Drawing.Size(133, 21); this.txtSubstringResault.TabIndex = 24; // // txtRemove // this.txtRemove.Location = new System.Drawing.Point(278, 331); this.txtRemove.Name = "txtRemove"; this.txtRemove.Size = new System.Drawing.Size(100, 21); this.txtRemove.TabIndex = 25; // // txtRemoveResault // this.txtRemoveResault.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtRemoveResault.Location = new System.Drawing.Point(394, 331); this.txtRemoveResault.Multiline = true; this.txtRemoveResault.Name = "txtRemoveResault"; this.txtRemoveResault.ReadOnly = true; this.txtRemoveResault.Size = new System.Drawing.Size(618, 21); this.txtRemoveResault.TabIndex = 26; // // txtReplaceChars // this.txtReplaceChars.Location = new System.Drawing.Point(278, 370); this.txtReplaceChars.Name = "txtReplaceChars"; this.txtReplaceChars.Size = new System.Drawing.Size(176, 21); this.txtReplaceChars.TabIndex = 27; // // txtReplaceWith // this.txtReplaceWith.Location = new System.Drawing.Point(570, 369); this.txtReplaceWith.Name = "txtReplaceWith"; this.txtReplaceWith.Size = new System.Drawing.Size(442, 21); this.txtReplaceWith.TabIndex = 28; // // txtReplaceResault // this.txtReplaceResault.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.txtReplaceResault.Location = new System.Drawing.Point(278, 407); this.txtReplaceResault.Multiline = true; this.txtReplaceResault.Name = "txtReplaceResault"; this.txtReplaceResault.ReadOnly = true; this.txtReplaceResault.Size = new System.Drawing.Size(734, 21); this.txtReplaceResault.TabIndex = 29; // // btnExit // this.btnExit.Location = new System.Drawing.Point(920, 455); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(92, 23); this.btnExit.TabIndex = 30; this.btnExit.Text = "&Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // lblIndexOf // this.lblIndexOf.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblIndexOf.Location = new System.Drawing.Point(394, 202); this.lblIndexOf.Name = "lblIndexOf"; this.lblIndexOf.Size = new System.Drawing.Size(470, 23); this.lblIndexOf.TabIndex = 31; // // lblLastIndexOf // this.lblLastIndexOf.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblLastIndexOf.Location = new System.Drawing.Point(394, 247); this.lblLastIndexOf.Name = "lblLastIndexOf"; this.lblLastIndexOf.Size = new System.Drawing.Size(470, 23); this.lblLastIndexOf.TabIndex = 32; this.lblLastIndexOf.Text = " "; // // frmMain // this.ClientSize = new System.Drawing.Size(1034, 525); this.Controls.Add(this.lblLastIndexOf); this.Controls.Add(this.lblIndexOf); this.Controls.Add(this.btnExit); this.Controls.Add(this.txtReplaceResault); this.Controls.Add(this.txtReplaceWith); this.Controls.Add(this.txtReplaceChars); this.Controls.Add(this.txtRemoveResault); this.Controls.Add(this.txtRemove); this.Controls.Add(this.txtSubstringResault); this.Controls.Add(this.lblSubstring); this.Controls.Add(this.txtEndIndex); this.Controls.Add(this.txtStartIndex); this.Controls.Add(this.txtLastIndexOf); this.Controls.Add(this.txtLastChar); this.Controls.Add(this.txtSearchIndex); this.Controls.Add(this.txtSearchChar); this.Controls.Add(this.txtToLower); this.Controls.Add(this.txtToUpper); this.Controls.Add(this.txtLength); this.Controls.Add(this.txtInput); this.Controls.Add(this.btnTest_Click); this.Controls.Add(this.label12); this.Controls.Add(this.label11); this.Controls.Add(this.label10); this.Controls.Add(this.label9); this.Controls.Add(this.label8); this.Controls.Add(this.label7); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "frmMain"; this.Text = "String Test"; this.Load += new System.EventHandler(this.frmMain_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion //#region是C# 预处理器指令。 //#region 使您可以在使用 Visual Studio //代码编辑器的大纲显示功能时指定可展开或折叠的代码块。 public frmMain() { InitializeComponent(); } public static void Main() { frmMain main = new frmMain(); Application.Run(main); } private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void label6_Click(object sender, EventArgs e) { } private void btnTest_Click_Click(object sender, EventArgs e) { bool flag; int index; int start; int howmany; string temp; //find length txtLength.Text = txtInput.Text.Length.ToString(); //change cases txtToUpper.Text = txtInput.Text.ToUpper(); txtToLower.Text = txtInput.Text.ToLower(); //index of index = txtInput.Text.IndexOf(txtSearchChar.Text, 0); lblIndexOf.Text = "txtInput.Text.IndexOf(\"" + txtSearchChar.Text + "\",0)= "; txtSearchIndex.Text=index.ToString(); //last index of: index = txtInput.Text.LastIndexOf(txtLastChar.Text); lblLastIndexOf.Text = "txtInput.Text.LastIndexOf(\"" + txtLastChar.Text + "\")= "; txtLastIndexOf.Text = index.ToString(); //Substring flag = int.TryParse(txtStartIndex.Text, out start); if (flag == false) { MessageBox.Show("Inporter numric input. Re-enter"); txtStartIndex.Focus(); return; } flag = int.TryParse(txtEndIndex.Text, out howmany); if (flag == false) { MessageBox.Show("Inporter numric input. Re-enter"); txtEndIndex.Focus(); return; } lblSubstring.Text="txtInput.Text.Substring("+start.ToString()+","+howmany.ToString() +")="; txtSubstringResault.Text = txtInput.Text.Substring(start,howmany); //Remove temp = txtInput.Text; index = temp.IndexOf(txtRemove.Text); if (index > 0) { txtRemoveResault.Text = temp.Remove(index,txtRemoveResault.Text.Length); } //replace temp = txtInput.Text; txtReplaceResault.Text = temp.Replace(txtReplaceChars.Text, txtReplaceWith.Text); } private void frmMain_Load(object sender, EventArgs e) { } private void btnExit_Click(object sender, EventArgs e) { Close(); } }
以上是对字符串的诸多应用,下面对出现的诸多方法进行详述:
(1)数据包装器
在非OOP语言中,值类型和引用类型不能很好的融合在一起。另一方面C#从开始就被设计成面向对象编程语言。C#提供了风装置类型的包装器类。值包装器认为有一个属性(它的值)以及六个方法。
(2)修改字符串的大小写
ToUpper 和ToLower。
(3)查找特定的字符
IndexOf()
假设需要在一个字符串中查找特定的字符。(计算字符串数量的时候把第一个位置称为位置0)
index = txtInput.Text.IndexOf(txtSearchChar.Text, 0); lblIndexOf.Text = "txtInput.Text.IndexOf(\"" + txtSearchChar.Text + "\",0)= "; txtSearchIndex.Text=index.ToString();
indexof的两个参数值为:希望查找的字符串(txtsearchchar.text)和希望从哪个索引位置开始索引。
如果找不到与传递给他的字符序列完全匹配的字符,则他会返回-1。
LastIndexOf()搜索字符串的特定字符的最后一次出现的位置。
只有一个参数,就是他要搜索的字符。如果没有找到匹配字符,则返回 -1。
(4)搜索子字符串
txtSubstringResault.Text = txtInput.Text.Substring(start,howmany);
以上的代码,从字符串中的第start+1个字符开始搜索子字符串,并复制howmany个字符。
(5)删除子字符串
temp = txtInput.Text; index = temp.IndexOf(txtRemove.Text); if (index > 0) { txtRemoveResault.Text = temp.Remove(index,txtRemoveResault.Text.Length); }
Remove的参数:第一个是字符串中目标子字符串的位置,利用length属性得到要删除字符的个数。使程序更加灵活。
(6)删除子字符串
temp = txtInput.Text; txtReplaceResault.Text = temp.Replace(txtReplaceChars.Text, txtReplaceWith.Text);
第一行的复制输入字符串,从而不会永久地修改txtinput文本框中的text属性。
Rplace的两个参数:第一个参数是希望替换的字符串,第二个参数是希望取代第一个参数的字符串。如果 原始字符串中不存在目标字符串,那么就不会修改原始字符串。
这里写代码片
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 我是运营,我没有假期
- android上改变listView的选中颜色
- String.intern
- c#调用COM组件
- DB2数据库的安装
- Prototype源码浅析 String部分(二)
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- “传奇”图象数据存储方式
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程