StringWriter实现的一个功能

本文提供了一个使用C#进行文本处理的例子,展示了如何通过读取和写入操作将一组双倍间距的句子转换为连续段落,再还原为原始文本格式的过程。涉及到StringReader和StringWriter的使用。
    如何用一组双倍间距的句子创建一个连续的段落,然后将该段落重新转换为原来的文本:

None.gifusing System;
None.gif
using System.IO;
None.gif
None.gif
class StringRW
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string textReaderText = "TextReader is the abstract base " +
InBlock.gif            
"class of StreamReader and StringReader, which read " +
InBlock.gif            
"characters from streams and strings, respectively.\n\n" +
InBlock.gif
InBlock.gif            
"Create an instance of TextReader to open a text file " +
InBlock.gif            
"for reading a specified range of characters, or to " +
InBlock.gif            
"create a reader based on an existing stream.\n\n" +
InBlock.gif
InBlock.gif            
"You can also use an instance of TextReader to read " +
InBlock.gif            
"text from a custom backing store using the same " +
InBlock.gif            
"APIs you would use for a string or a stream.\n\n";
InBlock.gif
InBlock.gif        Console.WriteLine(
"Original text:\n\n{0}", textReaderText);
InBlock.gif
        string aLine, aParagraph = null;
InBlock.gif        StringReader strReader 
= new StringReader(textReaderText);
InBlock.gif        
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            aLine 
= strReader.ReadLine();
InBlock.gif            
if(aLine != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                aParagraph 
= aParagraph + aLine + " ";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                aParagraph 
= aParagraph + "\n";
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.WriteLine(
"Modified text:\n\n{0}", aParagraph);
InBlock.gif
        int intCharacter;
InBlock.gif        
char convertedCharacter;
InBlock.gif        StringWriter strWriter 
= new StringWriter();
InBlock.gif        strReader 
= new StringReader(aParagraph);
InBlock.gif        
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            intCharacter 
= strReader.Read();
InBlock.gif            
 if(intCharacter == -1break;
InBlock.gif             convertedCharacter 
= Convert.ToChar(intCharacter);
InBlock.gif            
if(convertedCharacter == '.')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWriter.Write(
".\n\n");
InBlock.gif               
strReader.Read();
InBlock.gif                strReader.Read();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWriter.Write(convertedCharacter);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.WriteLine(
"\nOriginal text:\n\n{0}"
InBlock.gif            strWriter.ToString());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/jigee/archive/2006/05/20/405151.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值