Guide pratique pour écrire des caractères dans une chaîne
Les exemples de code suivants écrivent les caractères d’un tableau dans une chaîne de façon synchrone et asynchrone.
Exemple : Écrire des caractères de manière synchrone dans une application console
L’exemple suivant utilise un StringWriter pour écrire cinq caractères dans un objet StringBuilder de façon synchrone.
using System;
using System.IO;
using System.Text;
public class CharsToStr
{
public static void Main()
{
StringBuilder sb = new StringBuilder("Start with a string and add from ");
char[] b = { 'c', 'h', 'a', 'r', '.', ' ', 'B', 'u', 't', ' ', 'n', 'o', 't', ' ', 'a', 'l', 'l' };
using (StringWriter sw = new StringWriter(sb))
{
// Write five characters from the array into the StringBuilder.
sw.Write(b, 0, 5);
Console.WriteLine(sb);
}
}
}
// The example has the following output:
//
// Start with a string and add from char.
Imports System.IO
Imports System.Text
Public Class CharsToStr
Public Shared Sub Main()
Dim sb As New StringBuilder("Start with a string and add from ")
Dim b() As Char = {"c", "h", "a", "r", ".", " ", "B", "u", "t", " ", "n", "o", "t", " ", "a", "l", "l"}
Using sw As StringWriter = New StringWriter(sb)
' Write five characters from the array into the StringBuilder.
sw.Write(b, 0, 5)
Console.WriteLine(sb)
End Using
End Sub
End Class
' The example has the following output:
'
' Start with a string and add from char.
Exemple : Écrire des caractères de manière asynchrone dans une application WPF
L’exemple suivant montre le code-behind d’une application WPF. Au chargement de la fenêtre, l’exemple lit tous les caractères de façon asynchrone à partir d’un contrôle TextBox et les stocke dans un tableau. Ensuite, il écrit de façon asynchrone chaque lettre ou espace blanc sur une ligne distincte d’un contrôle TextBlock.
using System;
using System.Text;
using System.Windows;
using System.IO;
namespace StringReaderWriter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
char[] charsRead = new char[UserInput.Text.Length];
using (StringReader reader = new StringReader(UserInput.Text))
{
await reader.ReadAsync(charsRead, 0, UserInput.Text.Length);
}
StringBuilder reformattedText = new StringBuilder();
using (StringWriter writer = new StringWriter(reformattedText))
{
foreach (char c in charsRead)
{
if (char.IsLetter(c) || char.IsWhiteSpace(c))
{
await writer.WriteLineAsync(char.ToLower(c));
}
}
}
Result.Text = reformattedText.ToString();
}
}
}
Imports System.IO
Imports System.Text
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Async Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim charsRead As Char() = New Char(UserInput.Text.Length) {}
Using reader As StringReader = New StringReader(UserInput.Text)
Await reader.ReadAsync(charsRead, 0, UserInput.Text.Length)
End Using
Dim reformattedText As StringBuilder = New StringBuilder()
Using writer As StringWriter = New StringWriter(reformattedText)
For Each c As Char In charsRead
If Char.IsLetter(c) Or Char.IsWhiteSpace(c) Then
Await writer.WriteLineAsync(Char.ToLower(c))
End If
Next
End Using
Result.Text = reformattedText.ToString()
End Sub
End Class
Voir aussi
- StringWriter
- StringWriter.Write
- StringBuilder
- E/S de fichier et de flux
- E/S sur fichier asynchrones
- Guide pratique pour énumérer des répertoires et des fichiers
- Guide pratique pour lire et écrire dans un fichier de données créé récemment
- Guide pratique pour ouvrir un fichier journal et y ajouter des éléments
- Comment : lire du texte dans un fichier
- Comment : écrire du texte dans un fichier
- Guide pratique pour lire les caractères d’une chaîne
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de l’année 2024, nous abandonnerons progressivement le mécanisme de retour d’information GitHub Issues pour le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultez :Soumettre et afficher des commentaires pour