首页  >  文章  >  后端开发  >  C# 字符串编写器

C# 字符串编写器

WBOY
WBOY原创
2024-09-03 15:23:18392浏览

C#中的StringWriter类派生自TextWriter子类,可以使用StringWriter类操作字符串,并且该StringWriter类用于写入属于System.StringBuilder类。使用此 StringBuilder 类可以有效地构建文本命名空间和字符串,因为字符串在 C# 中是不可变的,并且 StringWriter 提供了 Write 和 WriteLine 方法,以便能够写入 StringBuilder 的对象,并且可以同步完成写入字符串异步方式,StringBuilder类存储StringWriter类写入的信息。

语法:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringWriter : TextWriter
C# StringWriter

工作和构造函数

为了理解C#中StringWriter类的工作原理,我们需要了解StringWriter类的构造函数、StringWriter类的属性和StringWriter类的方法。

  • StringWriter():使用 StringWriter() 方法初始化 StringWriter 类的新实例。
  • StringWriter(IFormatProvider):使用 (StringWriter(IFormatProvider) 方法初始化 StringWriter 类的新实例,并将格式控制指定为参数。
  • StringWriter(StringBuilder):使用 StringWriter(IFormatProvider) 方法初始化 StringWriter 类的新实例,并将格式控制指定为参数。
  • StringWriter(StringBuilder,?IFormatProvider): StringWriter 类的新实例被初始化以写入指定为第一个参数的 StringBuilder,并将格式提供程序指定为第二个参数。

C# StringWriter 类的属性

StringWriter类有几个属性。它们的解释如下:

  • 编码:对 C# 中 StringWriter 类的属性进行编码用于获取我们写入输出的编码。
  • FormatProvider:C#中StringWriter类的FormatProvider属性用于获取进行格式控制的对象。
  • NewLine:C#中StringWriter类的NewLine属性用于获取或设置行结束符字符串,该字符串行结束符由当前TextWriter使用。

C# StringWriter 类的方法

StringWriter类有多种方法。它们的解释如下:

1。 Close(): 可以使用 Close() 方法关闭 StringWriter 和流。

2。 Dispose(): TextWriter对象使用的所有资源都可以使用dispose()方法释放。

3。 Equals(Object):Equals(Object)方法用于判断指定对象是否等于当前对象。

4。 Finalize():对象可以使用 Finalize() 方法释放自身占用的资源并执行其他清理操作。

5。 GetHashCode():GetHashCode()方法默认可以用作哈希函数。

6。 GetStringBuilder(): 使用 GetStringBuilder() 方法返回底层 StringBuilder。

7。 ToString():使用 ToString() 方法将由字符组成的字符串返回给 StringWriter。

8。 WriteAsync(String): 使用 WriteAsync(String) 方法将字符串异步写入指定为参数的字符串。

9。 Write(Boolean):指定为参数的布尔值以文本形式表示,并使用 Write(Boolean) 方法写入字符串。

10。 Write(String):使用 Write(String) 方法将字符串写入指定为参数的当前字符串。

11。 WriteLine(String):使用 WriteLine(String) 方法将后跟行终止符的字符串写入指定为参数的当前字符串。

12。 WriteLineAsync(): 使用 WriteLineAsync(String) 方法将后跟行终止符的字符串异步写入指定为参数的当前字符串。

C# StringWriter 的实现示例

以下是 C# StringReader 类的示例:

示例#1

代码:

using System
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program
{
class Check
{
//calling the main method
static void Main(string[] args)
{
//define a string to hold the path of the file containing data
String str = @"D:\Ex.txt";
//create an instance of the stream writer class and pass the string containing the path  of the file to appendtext() method.
using (StreamWriter sw = File.AppendText(str))
{
//using the instance of the stringwriter class, write the data to the file
sw.WriteLine("Welcome to StringWriter class");
sw.Close();
//using the string containing the path of the file, the contents of the file are read
Console.WriteLine(File.ReadAllText(str));
}
Console.ReadKey();
}
}
}

输出:

C# 字符串编写器

In the above program, a namespace called the program is declared. Then the main method is called. Then a string is declared which holds the path of the file in which the data will be written. Then an instance of the StringWriter method is created which is assigned to the appendtext() method to which the string containing the path of the file is passed as a parameter. Using the instance of the StringWriter class that was just created, data is written to the file Ex.txt. Here the data written is “Welcome to StringWriter class.” Then the instance of the StringWriter class is closed using the Close() method. Then using the string containing the path of the file, the contents of the file are read and the same is displayed in the output.

Example #2

C# program to demonstrate usage of WriteLine() method of StringWriter class.

Code :

using System;
using System.IO;
using System.Text;
namespace Program
{
class Check
{
//Main method is called
static void Main(string[] args)
{
//define a string to hold the data to be displayed
string str = "Hello, Welcome to the StringWriter class \n" +
"This tutorial is for learning \n" +
"Learning is fun";
// An instance of the string builder class is created
StringBuilder build = new StringBuilder();
// an instance of the stringwriter class is created and the instance of the     stringbuilder class is passed as a parameter to stringwriter class
StringWriter write = new StringWriter(build);
// data is written using string writer writeline() method
write.WriteLine(str);
write.Flush();
// the instance of the stringwriter is closed
write.Close();
// an instance of stringreader class is created to which the instance of stringbuilder  class is passed as a parameter
StringReader read = new StringReader(build.ToString());
while (read.Peek() > -1)
{
Console.WriteLine(read.ReadLine());
}
}
}
}

Output:

C# 字符串编写器

Conclusion

In this tutorial, we understand the concept of StringWriter class in C# through definition, constructors of StringWriter class, properties of StringWriter class, and methods of StringWriter class, working of StringWriter class through programming examples and their outputs demonstrating the methods of StringWriter class.

以上是C# 字符串编写器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# StringReader下一篇:C# StreamReader