今日作业:
1.画图表示下面代码在内存中的分配(描述出字符串驻留的相关概念)?
string str1 = "123";
string str2 = str1 + "456";
string str3 = "123456"
2.使用C# 实现 文本取左边() 文本取右边() 文本取中间()
using System;
using System.Text;
namespace vscodeprogect
{
class Program
{
static void Main()
{
string str22="aaaads左fadfafklakfj中alkjdkfa右djlasdjflk";
//取左边
System.Console.WriteLine(Left(str22,"左"));
System.Console.WriteLine(Right(str22,"右"));
System.Console.WriteLine(Takerthe(str22,"左","右"));
}
//文本_取左边
public static string Left(string str,string s)
{
string str1 = str.Substring(0,str.IndexOf(s));
return str1;
}
//文本_取右边
public static string Right(string str,string s)
{
string str1 = str.Substring(str.IndexOf (s)+s .Length);
return str1;
}
//文本_取中间
public static string Takerthe(string str,string lstr ,string rstr)
{
int x = str.IndexOf(lstr)+lstr.Length;
string str1 = str.Substring(x,str.IndexOf(rstr,x)-x);
return str1;
}
}
}
3.请用C# 实现 utf16到 GBK 的转换
static string UTF16ConvertGBK(string text)
{
Encoding gbk = Encoding.GetEncoding("gbk");
Encoding utf16 = Encoding.GetEncoding("utf-16");
byte[] sourceByte = utf16.GetBytes(text);
sourceByte = Encoding.Convert(utf16, gbk, sourceByte);
return gbk.GetString(sourceByte);
}
|