簡體   English   中英

在最后一個空格之后替換字符串的結尾

[英]Replacing the end of a string after the last space

有沒有辦法可以替換字符串的最后一句話? 就像最后一個空格之后的最后一個字。 並通過發送一些{SPACE}鍵來代替它? 我想做的是將句子的日期時間放在文本框的末尾,將其右對齊

我沒有嘗試過任何代碼,因為我什至不知道從哪里開始。 我對我要執行的操作的異議就像是Skypes消息之類的東西? 隨着日期時間的結束。

是否有一些教程可以幫助某些人說我應該使用的女巫?

這是Skypes:

在此處輸入圖片說明

這是我的:

在此處輸入圖片說明

您提到了替換字符串的最后一個SENTENCE-以及最后一個WORD。 你想做什么?

要替換字符串中的最后一個單詞,可以執行以下操作:

var sentence = "scary solution even WITH a monospaced font";
sentence = sentence.Substring(0, sentence.LastIndexOf(' ')) + " CRAZY";

但是,如果您希望將日期/時間放置在相對於某些文本的特定位置,則添加空格是一種非常令人震驚的糟糕方法。

請參閱下面的我的解決方案。 假定您正在使用設置為等寬字體的多行文本框。 它使用復合格式將用戶名和消息對齊/截斷為由userMaxWidthmsgMaxWidth字段確定的多個字符。 這是一個笨拙的解決方案,消息的msgMaxWidth為單行msgMaxWidth字符。 我不建議將其作為嚴肅的解決方案。 相反,我將看一個自定義控件。

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            ChatForm chatForm = new ChatForm();
            chatForm.PostMessage("Ash", "test",
                DateTime.Today + new TimeSpan(18, 0, 0));
            chatForm.PostMessage("Bob", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                DateTime.Today + new TimeSpan(18, 5, 0));
            Application.Run(chatForm);
        }
    }

    class ChatForm : Form
    {
        public ChatForm()
        {
            InitializeComponent();
        }

        private int userMaxWidth = 8;
        private int msgMaxWidth = 40;

        public void PostMessage(string user, string msg, DateTime time)
        {
            // Truncate user
            if (user.Length > userMaxWidth)
                user = user.Substring(0, userMaxWidth);
            // Trucate msg
            if (msg.Length > msgMaxWidth)
                msg = msg.Substring(0, msgMaxWidth);
            string compositeString = 
                "#{0,-" + userMaxWidth + "}{1,-" + msgMaxWidth + "}{2:HH:mm}";
            string formattedMsg =
                String.Format(compositeString, user, msg, time);

            textBox.AppendText(formattedMsg);
            textBox.AppendText(Environment.NewLine);
            textBox.AppendText(Environment.NewLine);
        }

        private System.Windows.Forms.TextBox textBox;

        private void InitializeComponent()
        {
            this.textBox = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox
            // 
            this.textBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox.Font = new System.Drawing.Font("Courier New", 8.25F,
                System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox.Location = new System.Drawing.Point(0, 0);
            this.textBox.Multiline = true;
            this.textBox.WordWrap = false;
            this.textBox.Name = "textBox";
            this.textBox.Size = new System.Drawing.Size(500, 200);
            this.textBox.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(500, 200);
            this.Controls.Add(this.textBox);
            this.Name = "ChatForm";
            this.Text = "ChatForm";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    }
}

textBoxText屬性如下:

#Ash     test                                    18:00

#Bob     Lorem ipsum dolor sit amet, consectetur 18:05

您不應該將數據與其他數據混為一談以用於顯示目的。

string message = "@Bob This is my message                           18:05";

那不是您能夠輕松,干凈地實現顯示的目的。 您應該將這些消息實現為一個類:

public class Message
{
    public string Username { get; set; }
    public string Message { get; set; }
    public DateTime TimeStamp { get; set; }
}

然后,您將使用自定義UserControl或ItemsControl定義此結構化數據的視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM