用换行符替换字典值

Replace Dictionary Value with NewLine

我目前正在尝试添加自定义工具提示作为我 Visual Studio 的扩展。

然而,当鼠标悬停在关键字上方时,(因为我有很长的描述,我尝试使用 \r\n 但它显然不起作用并且会在工具提示中显示为 \r\n 所以我使用)

//var reader = new StreamReader(File.OpenRead(@"C:\xx\QuickInfo.csv"));
/*while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(',');
        //add in values in position 0 and 1 
        m_dictionary.Add(values[0], values[1].Replace("<br>", Environment.NewLine));
    }*/

space

internal class VCLToolTipSource: IQuickInfoSource
{
    private VCLToolTipSourceProvider m_provider;
    private ITextBuffer m_subjectBuffer;
    private Dictionary<string, string> m_dictionary;
    public VCLToolTipSource(VCLToolTipSourceProvider provider, ITextBuffer subjectBuffer)
    {
        m_provider = provider;
        m_subjectBuffer = subjectBuffer;

        m_dictionary = new Dictionary<string, string>();

        //CSV for Quick Info
        //var reader = new StreamReader(File.OpenRead(@"C:\xx\QuickInfo.csv"));
        //For going through CSV positions 0,1//
        /*while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(',');
            //add in values in position 0 and 1 
            m_dictionary.Add(values[0], values[1].Replace("<br>", Environment.NewLine));
        }*/

        //List of all tool tips//


        m_dictionary.Add("keyword", "value");
        m_dictionary.Add("adapt", " - Process given file  <br>Syntax: #adapt[(-samelevel|-continue|-samelevel-continue|-copy)][:] path [output-folder] [output-file]( vcl-command )*[#endadapt]<br>Variations:<br><br> -samelevel :  The scope of the adapted file will be raised to the current level which makes it possible to override variables<br> -continue  :  If the adaptable file is not found continue processing. (warning message instead of stop processing with error)<br>Note: #adapt-continue will open the file and process it. Unlike #adapt-copy.<br> -copy :  Instead of processing the file");

这是将所有的“<br>”替换为Environment.Newline以缩短它。

但是,既然我不想使用 CSV 文件导入数据,如何从字典中读取数据并将 <br> 替换为换行符? ?

谢谢

您是否尝试在字符串末尾添加 yourstring.Replace("<br>", Environment.NewLine)。

m_dictionary
    .Add("adapt", 
         " - Process given file  <br>Syntax: #adapt[(-samelevel|-continue|-samelevel-continue|-copy)][:] path [output-folder] [output-file]( vcl-command )*[#endadapt]<br>Variations:<br><br> -samelevel :  The scope of the adapted file will be raised to the current level which makes it possible to override variables<br> -continue  :  If the adaptable file is not found continue processing. (warning message instead of stop processing with error)<br>Note: #adapt-continue will open the file and process it. Unlike #adapt-copy.<br> -copy :  Instead of processing the file"
    .Replace("<br>", Environment.NewLine));

将“<br>"替换为Environment.NewLine\n

代码:

string str =  " - Process given file  <br>Syntax: #adapt[(-samelevel|-continue|-samelevel-continue|-copy)][:] path [output-folder] [output-file]( vcl-command )*[#endadapt]<br>Variations:<br><br> -samelevel :  The scope of the adapted file will be raised to the current level which makes it possible to override variables<br> -continue  :  If the adaptable file is not found continue processing. (warning message instead of stop processing with error)<br>Note: #adapt-continue will open the file and process it. Unlike #adapt-copy.<br> -copy :  Instead of processing the file";
   str =  str.Replace("<br>", Environment.NewLine);

str =  str.Replace("<br>", "\n");

输出: - 处理给定文件
语法:#adapt[(-samelevel|-continue|-samelevel-continue|-copy)][:] path [output-folder] output-file*[#endadapt] 变化:

-samelevel : 改编文件的范围将提升到当前级别,这使得覆盖变量成为可能 -continue :如果没有找到适配文件继续处理。 (警告消息而不是停止处理错误) 注意:#adapt-continue 将打开文件并处理它。不像#adapt-copy。 -copy : 而不是处理文件

虽然我上面的回答运行在理想情况下很好,但你必须涵盖所有情况。

public string ReplaceBRwithNewline(string txtVal)  
{  
    string newText = "";  
    // Create Regex    
    System.Text.RegularExpressions.Regex regex =   
        new System.Text.RegularExpressions.Regex(@"(<br />|<br/>|</ br>|</br>)");  
    // Replace new line with <br/> tag    
    newText = regex.Replace(txtVal, "\r\n");  
    // Result    
    return newText;  
}

我将此处替换为 \r\n,因为在某些浏览器上它适用于 \n,而在某些浏览器上它适用于 \r\n

也许这更符合你想做的事情?

m_dictionary = m_dictionary.ToDictionary(
                                k => k.Key, 
                                v => v.Value.Replace("<br>", Environment.NewLine));

这将遍历整个字典并将任何 "<br>" 值替换为 Environment.NewLine