Comment : lire les caractères d’une chaîne
Les exemples de code suivants montrent comment lire des caractères de façon synchrone et asynchrone à partir d’une chaîne.
Lire des caractères de façon asynchrone
Cet exemple lit 13 caractères de façon synchrone à partir d’une chaîne, les stocke dans un tableau, puis les affiche. Ensuite, l’exemple lit les caractères restants de la chaîne, les stocke dans le tableau à partir du sixième élément, puis affiche le contenu du tableau.
using System;
using System.IO;
public class CharsFromStr
{
public static void Main()
{
string str = "Some number of characters";
char[] b = new char[str.Length];
using (StringReader sr = new StringReader(str))
{
// Read 13 characters from the string into the array.
sr.Read(b, 0, 13);
Console.WriteLine(b);
// Read the rest of the string starting at the current string position.
// Put in the array starting at the 6th array member.
sr.Read(b, 5, str.Length - 13);
Console.WriteLine(b);
}
}
}
// The example has the following output:
//
// Some number o
// Some f characters
Imports System.IO
Public Class CharsFromStr
Public Shared Sub Main()
Dim str As String = "Some number of characters"
Dim b(str.Length - 1) As Char
Using sr As StringReader = New StringReader(str)
' Read 13 characters from the string into the array.
sr.Read(b, 0, 13)
Console.WriteLine(b)
' Read the rest of the string starting at the current string position.
' Put in the array starting at the 6th array member.
sr.Read(b, 5, str.Length - 13)
Console.WriteLine(b)
End Using
End Sub
End Class
' The example has the following output:
'
' Some number o
' Some f characters
Exemple : Lire des caractères de façon asynchrone
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
- StringReader
- StringReader.Read
- E/S sur fichier asynchrones
- Comment : créer une liste de répertoires
- Comment : 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
- Comment : écrire des caractères dans une chaîne
- E/S de fichier et de flux
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour