C# stiring 专题.docx
- 文档编号:8192337
- 上传时间:2023-01-29
- 格式:DOCX
- 页数:14
- 大小:20.55KB
C# stiring 专题.docx
《C# stiring 专题.docx》由会员分享,可在线阅读,更多相关《C# stiring 专题.docx(14页珍藏版)》请在冰豆网上搜索。
C#stiring专题
C#stiring专题
1.c#字符串替换
把"2007年11月14日"
转换成
"2007/11/14"
stringmystring=
"2007年11月14日";
mystring=
mystring.Replace("年","/").Replace("月","/").Substring(0,9);
Response.Write(mystring);
2.C#字符串转换为整数
charc=
Convert.ToChar(TextBox1.Text);
intd=(int)c;
MessageBox.Show(d.ToString());
(C#)字符串实现累加
(如a+="123")
都是可以的
4.C#字符串数组转换
(如string[]->string)
string[]tt;
string
t="";
for(inti=0;i<tt.length;i++)
{
t+=tt[i];
}
5.c#从字符串中取字符串
如要取字符串"fslakdfjldj"中的后三位
String
结果=源字符串.Substring(源字符串.Length-3,3);
String.Substring(Int32,Int32)方法
从此实例检索子字符串。
子字符串从指定的字符位置开始且具有指定的长度。
参数:
startIndex子字符串的起始位置的索引。
length子字符串中的字符数。
6.c#字符串替换问题
if(rtxtMain.SelectionStart<
rtxtMain.Text.Length)
{
if(Uper.Equals(true))
{
try
{
rtxtMain.SelectedText=rep;
rtxtMain.SelectionStart=
rtxtMain.Find(selec,
rtxtMain.SelectionStart+rtxtMain.SelectionLength,rtxtMain.Text.Length,
RichTextBoxFinds.MatchCase);
}
catch(Exceptione)
{
MessageBox.Show("下文找不到"+selec+",将从头查找!
","查找提示",
MessageBoxButtons.OK,MessageBoxIcon.Information);
rtxtMain.SelectionStart=0;
}
}
else
{
try
{
rtxtMain.SelectedText=rep;
rtxtMain.SelectionStart=
rtxtMain.Find(selec,rtxtMain.SelectionStart+rtxtMain.SelectionLength,
rtxtMain.Text.Length,RichTextBoxFinds.None);
}
catch(Exception
e)
{
MessageBox.Show("下文找不到"+selec+",将从头查找!
","查找提示",
MessageBoxButtons.OK,MessageBoxIcon.Information);
rtxtMain.SelectionStart=0;
}
}
}
else
{
MessageBox.Show("找不到"+selec+"!
","查找提示",MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
为什么在替换的时候比如fffff当鼠标在fff替换ff的时候不能替换?
也就是说不能替换文本中的最后正确的字符串。
怎么修改呢?
还有就是如何显示打印预览?
if里语句
rtxtMain.SelectionStart<rtxtMain.Text.Length
改成:
rtxtMain.SelectionStart<=
rtxtMain.Text.Length
7.C#中的字符串复制函数
要将str1的复制到str2中去
要加using
System.***吗?
你应该在MSDN里面找方法啊!
一、String.CopyTo方法
将指定数目的字符从此实例中的指定位置复制到
Unicode字符数组中的指定位置。
语法:
publicvoidCopyTo(intsourceIndex,char[]
destination,intdestinationIndex,intcount)
参数:
sourceIndex:
此实例中的字符位置。
destinationUnicode:
字符的数组。
destinationIndexdestination:
中的数组元素。
count:
此实例中要复制到destination的字符数。
示例:
usingSystem;
publicclass
CopyToTest{
publicstaticvoidMain(){
//Embedanarrayofcharacters
inastring
stringstrSource="changed";
char[]destination={'T',
'h','e','','i','n','i','t','i','a','l','',
'a','r','r','a',
'y'};
//Printthechararray
Console.WriteLine(destination);
//
Embedthesourcestringinthedestinationstring
strSource.CopyTo(0,
destination,4,strSource.Length);
//Printtheresultingarray
Console.WriteLine(destination);
strSource="Adifferentstring";
//Embedonlyasectionofthesourcestringinthedestination
strSource.CopyTo(2,destination,3,9);
//Printtheresultingarray
Console.WriteLine(destination);
}
}
二、String.Copy方法
创建一个与指定的String具有相同值的String的新实例。
命名空间:
System
程序集:
mscorlib(在
mscorlib.dll中)
语法:
publicstaticstringCopy(stringstr)
参数:
str:
要复制的String。
返回值:
与str具有相同值的新String。
示例:
下面的代码示例显示了由两个变量引用的两个不同的字符串,创建第一个字符串的副本,将对新字符串的引用赋给第二个变量,然后显示由两个变量引用的两个字符串,以演示两个字符串现在是相同的。
//SampleforString.Copy()
usingSystem;
classSample{
public
staticvoidMain(){
stringstr1="abc";
stringstr2="xyz";
Console.WriteLine("1)str1='{0}'",str1);
Console.WriteLine("2)str2=
'{0}'",str2);
Console.WriteLine("Copy...");
str2=String.Copy(str1);
Console.WriteLine("3)str1='{0}'",str1);
Console.WriteLine("4)str2=
'{0}'",str2);
}
}
/*
Thisexampleproducesthefollowing
results:
1)str1='abc'
2)str2='xyz'
Copy...
3)str1='abc'
4)str2='abc'
*/
8.C#中将字符串转化为数据
比如,一个字符串stringa=123.0254
怎样转化成一个数据floatb=123.0254
stringa="123.0254";
floatb=
Convert.ToSingle(a);
MessageBox.Show(b.ToString());
float实际上对应与System.Single
9.c#中如何取字符串最左边和最右边的字符
stringleft=
str.Substring(0,1);
stringright=
str.Substring(str.Lenght,1);
10.c#对字符串进行运算
比如我要把:
"abcd"变成"bcde"
using
System;
usingSystem.Security.Cryptography;
usingSystem.Text;
staticvoidMain()
{
try
{
stringorigin="abcd";
UnicodeEncodingByteConverter=newUnicodeEncoding();
byte[]
dataToEncrypt=ByteConverter.GetBytes(origin);
byte[]encryptedData;
byte[]decryptedData;
RSACryptoServiceProviderRSA=new
RSACryptoServiceProvider();
encryptedData=
RSA.Encrypt(dataToEncrypt,true);
decryptedData=
RSA.Decrypt(encryptedData,true);
Console.WriteLine("Decryptedplaintext:
{0}",ByteConverter.GetString(decryptedData));
}
catch(ArgumentNullException)
{
Console.WriteLine("Encryption
failed.");
}
}
11.c#中截取字符串
publicstaticstringCutString(string
inputString,intlen)
{
ASCIIEncodingascii=newASCIIEncoding();
int
tempLen=0;
stringtempString="";
byte[]s=ascii.GetBytes(inputString);
for(inti=0;i<s.Length;i++)
{
if((int)s[i]==63)
{
tempLen+=2;
}
else
{
tempLen+=1;
}
try
{
tempString+=inputString.Substring(i,1);
}
catch
{
break;
}
if(tempLen>len)
break;
}
//如果截过则加上半个省略号
byte[]
mybyte=System.Text.Encoding.Default.GetBytes(inputString);
if(mybyte.Length>len)
tempString+="";
returntempString;
12.在C#中怎样判断字符串是双精度型
doubled;boolb;
b=double.TryParse(某字符串,out
d);
if(b){MessageBox.Show("是双精度型d="+d.ToString());}
else{
MessageBox.Show("不是双精度型d="+d.ToString());
}
13.C#中获取路径字符串的上一级路径
如路径字符串“C:
\ABC\Temp\DC\”
我现在想取得“C:
\ABC\”
自已可以编写函数按“\”进行截取
publicstringgetName(stringpath)
{
return
path.substring(path.lastindexof("\"),path.length-path.lastindexof('\')-1);
}
MessageBox.Show(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryNameSystem.IO.Path.GetDirectoryName(@"C:
\ABC\Temp\DC\"))));
14.C#实现将字符串“A,B,C”按逗号分隔存储
stringstrABC;
strABC="A,B,C";
1String.Split
FirstName,Last
Name,Age,Phone,Email,Address
那么当你把当前行读取到一个string变量后,你可以这样进行分割:
stringcurrentLine=...;
string[]items=currentLine.Split(new
string[]{","},StringSplitOptions.None);
上面那行分割后的结果是:
FirstName
LastName
Age
Phone
Address
2
string
a="1,2,3,4,5,6,7,8,9";
string[]b=a.Split(newChar[]{','});
for
(inti=0;i<b.Length;i++)
Console.WriteLine(b[i]);
Console.Read();
15.字符串匹配问题c#
str="abc$text1$efg$text2$hi"
如何把字符串&text1&和$text2$从str中取出
stringdelimStr="$";
char[]
delimiter=delimStr.ToCharArray();
stringwords="abc$text1$efg$text2$hi";
string[]split=null;
Console.WriteLine("Thedelimitersare-{0}-",
delimStr);
split=words.Split(delimiter);
foreach(stringsinsplit)
{
if(s.Substring(0,2).Equals("te"))
Console.WriteLine("-{0}-",s);
}
-------------
16.如何删除字符串中的双引号c#
string.Replace('"','')
或
string.Replace("\"","")
stringstr1="I";
stringstr2="you";
stringstr3=str1+"love"+str2;
stringstr4=str1+str2;
stringstr5="I"+""
+"you";
注:
字符串和字符串用“+”连接。
字符串写在""中。
17.C#类型关键字全集说明
说明格式如下:
(type
说明文字.ef:
typ实例(使用或创建例子))
例如:
string字符串型.ef:
stringstr="";
string[]
字符串数组.ef:
string[]a={1,2,3,};
int整型.ef:
inta=25;
参考地址:
在C#中可以定义两种变量类型:
值类型和引用类型。
值类型存储实际值,而引用类型则为对存储在内存中某处的值的引用。
值类型在堆栈上分配,大多数程序语言都能应用它。
引用类型在堆上分配,通常代表类实例。
在C#代码中还可以定义自己的值和引用类型。
所有值和引用类型都由一个名为object的基本类发展而来。
在C#中还可以通过隐性转换(不会造成数据丢失)或显性转换(可能造成数据丢失或降低精确度)来改变数据类型。
预定义C#值类型
:
sbyte:
存储8位带符号整数。
sbyte中的s代表带符号(signed),意味着变量可为正值或负值。
sbyte变量的最小可能值为-128,最大可能值为127。
byte:
存储8位无符号整数。
与sbyte变量不同,byte变量不带符号,并且只能存储正数值。
byte变量的最小可能值为0,最大可能值为225。
short:
存储16带符号整数。
short变量的最小可能值为32768,最大可能值为32767。
int:
存储32位带符号整数。
int变量的最小可能值为-2147483648,最大可能值为2147683647。
uint:
存储32位无符号整数。
unit中的u代表无符号。
uint变量的最小可能值为0,最大可能值为4294967295。
long:
存储64位带符号整数。
long变量的最小可能值为-9223372036845775808,最大可能值为9223372036845775807。
ulong:
存储64位无符号整数。
ulong中的u代表无符号。
ulong变量的最小可能值为0,最大可能值为184********709551615。
char:
存储16位Unicode字符。
Char变量的最小可能值为值为0的Unicode字符,最大可能值为值为655535的Unicode字符。
float:
存储32位还符号浮点值。
float变量的值介于±1.5x10-45到±3.4x1038之间。
double:
存储64位带符号浮点值。
double变量的值介于±5.0x10-324到±1.7x10308之间。
decimal:
存储128位带符号浮点值。
decimal类型变量适用于金融计算。
decimal类型变量的值介于±1.0x10-28到±7.9x
1028之间。
bool:
存储两个可能值——真或假——中的一个。
应用bool类型变量是C#与C和C++不同的一个地方。
在C和C++中,整数值0与假同义,而任何非零值与真同义。
但在C#中,这些类型不再同义。
你不能将一个整数变量转换成一个等同的bool值。
如果你需要应用一个表示真或假条件的变量,应使用bool变量而非int变量。
预定义C#引用类型
string:
表示一个Unicode字符串。
用它可轻松操作并分配字符串。
字符串不可变,也就是说它一旦建立就不能修改。
因此当你试图修改一个字符串,如将它与另一个字符串连接时,会建立一个新字符串对象来存储新生成的字符串。
object:
表示一个通用类型。
在C#中,所有的预定义和用户定义的类型都由object类型或System.Object类发展而来。
每个类都创造了有自己的名字空间,指方法和变量可以知道彼此的存在,可以使用。
public类不但可以被同一程序包中的其它类使用,别的程序包中的类也可以使用;
变量和方法的修饰字public、protected、private:
public:
任何其他类、对象只要可以看到这个类的话,那么它就可以存取变量的数据,或使用方法。
protected变量和方法:
如果一个类中变量或方法有修饰字protected,同一类、同一包可以使用。
不同包的类要使用,必须是该类的子类,可以存取变量或调用。
同包的类,不同包但是是子类
private不允许任何其他类存取和调用;
当子类中的变量名与父类的相同,原来的变量被遮盖。
方法的覆盖(overriding)和重载(overloading)。
子孙类中定义的方法和祖先类中某个方法同名、同参数行,则祖先类中的该方法被覆盖;方法的重载是指一个对象的多态性,即多个方法用相同的名称,但参数行不同。
final:
final在方法之前,防止该方法被覆盖;
final在类之前,标是该类不能被继承;
final在变量之前,定义一个常量。
static:
在变量或方法之前,表明它们是属于类的;
静态变量在各实例间共享,如果是public静态变量,则其它类可以不通过实例化访问它们;
静态方法称为类的方法,因此不用实例化即可调用(面向过程)
一个对象的方法可以访问对象的数据成员,尽管不属于方法的局部变量;一个类的方法只能访问自己的局部变量。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# stiring 专题