引用数据类型---字符串变量
来源:互联网 发布: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使用字符串变量
字符串变量是允许存储和操作文本数据的引用变量。
字符串连接:
- <pre name="code" class="csharp">string name="Hailey";
- name=name+"Mohr";
字符串操作:
- txtLength.Text=txtInput.Text.Length.ToString();
字符串变量实际上是一个封装对象,它可以用点运算符打开。然后,通过点运算符可以使用该对象的属性和方法。
如:
- int length;
- string zip="80122";
- length=zip.Length;
属性和方法的重要区别:
记住:属性是与对象关联的变量,因此,它们不要求在名称后面有()。另一方面,所有方法必须再在方法名后面有();
实例:使用字符串类方法和属性
(1)字符串的长度:length属性
length属性存储字符串的长度。
- <pre name="code" class="csharp"><pre name="code" class="csharp">txtLength.Text=txtInput.Text.Length.ToString();
(2)修改字符串的大小写:ToUpper()和ToLower()
- txtToUpper.Text=txtInput.Text.ToUpper();
- txtToLower.Text=txtInput.Text.ToLower();
(3)查找特定的字符串:IndexOf()
假设需要在一个字符串中查找特定的字符。输入字符v并用IndexOf()方法查找该字符第一次出现的位置。
编程语言在字符串中计算字符数量时将第一个位置称为位置0,而不是位置1.
- index = textBox1.Text.IndexOf(textBox5.Text, 0);
- labelStingtotest.Text = "textBox1.Text.IndexOf(\"" + textBox12.Text + "\",0)=";
- textBox12.Text=index.ToString ();
(3)搜索字符的最后一次出现位置:LastIndexOf()
- index = textBox1.Text.LastIndexOf(textBox6.Text);
- labelStingtotest.Text = "textBox1.Text.LastIndexOf(\"" + textBox13.Text + "\")=";
- textBox13.Text = index.ToString();
(4)搜索字符串
使用Substring()f方法
- labeltxtInputTextSubstring.Text ="textBox1.Text.Substring("+start .ToString ()+","+howMany .ToString ()+")=";
- textBox15.Text =textBox1.Text.Substring(start,howMany);
(5)删除字符串
- temp=textBox1.Text;
- index=temp.IndexOf(textBox8.Text);
- if (index > 0)
- {
- textBox9.Text = temp.Remove(index, textBox8.Text.Length);
- }
(6)替换字符串
- temp=textBox1.Text;
- textBox11.Text =temp.Replace (textBox10.Text ,textBox16.Text);
- textBox11.Text =temp.Replace (“1234567890”,“***”);
完整代码示例:
- 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();
- }
- }
- }
- 引用数据类型---字符串变量
- 引用数据类型变量和基本数据类型变量
- 变量的数据类型及引用数据类型
- algrothm_基本数据类型和引用类型变量
- c++数据类型--指针、字符串、const、引用
- 2.变量/字符串/if/while/数据类型
- 如何在JS字符串中引用变量
- php中字符串引用变量或数组
- 变量赋值给另一个变量(基本数据类型和引用数据类型的)
- 基本数据类型、传递数组引用变量 的交换
- 第三章 primitive主数据类型和引用---认识变量
- 变量按数据类型分为基本类型和引用类型
- 输出字符串时引用变量(双引号字符串表达式内插)
- 字符串相等的比较方法,字符串的基本数据类型和引用数据类型。
- 引用数据类型
- 引用数据类型
- 引用数据类型
- 数据类型和引用数据类型
- windows下JAVA环境变量配置
- 反转链表
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- TCP:传输控制协议详解
- 利用三层交换机实现不同vlan间通信
- 引用数据类型---字符串变量
- 浅析__线段树延迟标记
- 堆和栈的区别详解
- Jenkins部署:SVN-Maven-Tomcat全程
- linux学习笔记2----userd命令大全
- 数据库设计(章节摘要)
- 使用DOM模型实现图片在div之间切换
- ACM技巧
- C语言中有用的宏和系统头文件