引用数据类型---字符串变量
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();
}
}
}
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();
}
}
}
相关文章推荐
- 17.2015.08.04第十八节课 C#2 (数值类型及调用、引用类型及调用、装拆箱、常量、变量、数据类型转换、算术运算符、赋值运算符、关系运算符、逻辑运算符、字符串的常用方法)
- C\C++ 程序员从零开始学习Android - 个人学习笔记(四) - java基础 - 数据类型、变量、字符串、数组
- C数据类型,变量,运算符,数组与字符串
- A Byte of Python 笔记(2)基本概念:数、字符串、转义符、变量、标识符命名、数据类型、对象
- ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值。
- 作为引用数据类型,数组变量与基本数据类型变量的使用有哪些区别?
- 语言基础(项目结构,数据类型,引用类型,转义字符,变量常量,基本的类型转换)
- 变量的数据类型及引用数据类型
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数
- Python教程学习简记1--Python数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set
- C++系列之基本数据类型、常量、变量、const、引用、typedef、枚举、类类型、编写自己的头文件
- Swift 2.0 学习第1天:常量和变量, 数据类型,字符和字符串
- 【C#】显示引用类型变量的内存地址(以字符串为例)
- algrothm_基本数据类型和引用类型变量
- 字符串常量与字符串变量 字符串与其他数据类型的任意转换
- c++数据类型(整型/浮点型/字符串/数组/引用/结构体(内存对齐)/类(虚函数))定义、所占字节数、最大最小值
- JAVA_SE基础——46.引用数据类型变量.值交换[独家深入解析]
- Python学习笔记(一)--Python基础知识:变量、数据类型、模块与字符串
- PHP之【变量的数据类型(重点是字符串和数组)】
- 基本数据类型、传递数组引用变量 的交换