C# 스트링라이터

WBOY
풀어 주다: 2024-09-03 15:23:18
원래의
321명이 탐색했습니다.

C#의 StringWriter 클래스는 TextWriter 하위 클래스에서 파생되며 StringWriter 클래스를 사용하여 문자열을 조작할 수 있으며 이 StringWriter 클래스는 System에 속하는 StringBuilder 클래스에 쓰는 데 사용됩니다. 문자열은 C#에서 변경할 수 없으며 Write 및 WriteLine 메서드는 StringWriter에서 제공되어 StringBuilder의 개체에 쓰고 문자열에 쓰는 작업이 동기적으로 수행될 수 있으므로 이 StringBuilder 클래스를 사용하여 텍스트 네임스페이스와 문자열을 효율적으로 구축할 수 있습니다. 비동기 방식이며 StringBuilder 클래스는 StringWriter 클래스에서 작성한 정보를 저장합니다.

구문:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringWriter : TextWriter
로그인 후 복사

C# StringWriter의 작업 및 생성자

C#에서 StringWriter 클래스의 작동 방식을 이해하려면 StringWriter 클래스의 생성자, StringWriter 클래스의 속성, StringWriter 클래스의 메서드를 이해해야 합니다.
  • StringWriter():
  • StringWriter 클래스의 새 인스턴스는 StringWriter() 메서드를 사용하여 초기화됩니다.
  • StringWriter(IFormatProvider):
  • StringWriter 클래스의 새 인스턴스는 매개변수로 지정된 형식 제어와 함께 (StringWriter(IFormatProvider) 메서드를 사용하여 초기화됩니다.
  • StringWriter(StringBuilder):
  • StringWriter 클래스의 새 인스턴스는 매개변수로 지정된 형식 제어와 함께 StringWriter(IFormatProvider) 메서드를 사용하여 초기화됩니다.
  • StringWriter(StringBuilder,?IFormatProvider):
  • StringWriter 클래스의 새 인스턴스는 첫 번째 매개변수로 지정된 StringBuilder에 쓰기 위해 초기화되고 형식 공급자는 두 번째 매개변수로 지정됩니다.

C# StringWriter 클래스의 속성

StringWriter 클래스에는 여러 가지 속성이 있습니다. 다음과 같이 설명됩니다:
  • 인코딩:
  • C#에서 StringWriter 클래스의 속성을 인코딩하는 것은 출력을 작성하는 인코딩을 가져오는 데 사용됩니다.
  • FormatProvider:
  • C#의 StringWriter 클래스의 FormatProvider 속성은 형식 제어를 수행하는 개체를 가져오는 데 사용됩니다.
  • NewLine:
  • C#에서 StringWriter 클래스의 NewLine 속성은 줄 종결자 문자열을 가져오거나 설정하는 데 사용되며 이 줄 종결자 문자열은 현재 TextWriter에서 사용됩니다.

C# StringWriter 클래스의 메서드

StringWriter 클래스에는 여러 가지 메서드가 있습니다. 다음과 같이 설명됩니다:

1. Close():

StringWriter와 스트림은 Close() 메서드를 사용하여 닫을 수 있습니다.

2. Dispose():

TextWriter의 객체에서 사용하는 모든 리소스는 dispose() 메서드를 사용하여 해제할 수 있습니다.

3. Equals(Object):

Equals(Object) 메서드는 지정된 개체가 현재 개체와 같은지 여부를 확인하는 데 사용됩니다.

4. Finalize():

객체는 자신이 차지한 리소스를 해제하고 Finalize() 메서드를 사용하여 다른 정리 작업을 수행할 수 있습니다.

5. GetHashCode():

기본적으로 GetHashCode() 메소드를 해시 함수로 사용할 수 있습니다.

6. GetStringBuilder():

기본 StringBuilder는 GetStringBuilder() 메서드를 사용하여 반환됩니다.

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!