简体   繁体   English

在C#中使用软连字符

[英]Use soft hyphen in C#

I would like to insert a soft hyphen between every letter in a word using C#. 我想使用C#在单词的每个字母之间插入一个软连字符。 for example here is some text: 例如,下面是一些文本:

Thisisatest => Thisisatest

'-' is a soft hyphen. “-”是软连字符。 How might i do this in C#? 我该如何在C#中做到这一点? I am going to use it in website. 我将在网站上使用它。

Note: I am on .NET 2.0. 注意:我在.NET 2.0上。

Use the HTML entity ­ 使用HTML实体­ for a soft hyphen: 对于软连字符:

theString = String.Join("­", theString.Select(c => c.ToString()));

For .NET 2.0: 对于.NET 2.0:

string[] chars = new string[theString.Length];
for (int i = 0; i < theString.Length; i++) {
  chars[i] = theString[i].ToString();
}
theString = String.Join("&shy;", chars);

Or using a StringBuilder : 或使用StringBuilder

StringBuilder builder = new StringBuilder(theString.Length * 6);
foreach (char c in theString) {
  builder.Append(c).Append("&shy;");
}
theString = builder.ToString(0, builder.Length - 5);

No LINQ required (but .NET 4.0 needed for the object[] overload in String.Join ): 不需要LINQ(但String.Joinobject[]重载需要.NET 4.0):

var test = "Thisisatest";
String.Join("&shy;", test.ToCharArray());

For poor souls on previous versions: 对于以前版本中的可怜灵魂:

String.Join("&shy;", test.Select(x => x.ToString()));

If you happen to use .NET 2.0 (as the OP has now stated), then please see another response as it doesn't need to be typed again. 如果您碰巧使用.NET 2.0(如OP现在所述),则请查看另一个响应,因为它无需再次键入。

try this 尝试这个

.net 3.5 .net 3.5

Console.WriteLine( String.Join("&shy;","Thisisatest".ToCharArray().Select(r=>r.ToString()).ToArray()));

.net 4.0+ .NET 4.0以上

Console.WriteLine(String.Join("&shy;", "Thisisatest".ToCharArray()));

Below is a simple helper method that you can use to add the hyphen between each letter for a given string. 下面是一个简单的辅助方法,您可以使用该方法在给定字符串的每个字母之间添加连字符。

    static string Hyphenify(string text)
    {
        return string.Join("-", text.ToArray());
    } 

You could also add another parameter to enable you to give the separator to use, see below. 您还可以添加另一个参数,以使您可以使用分隔符,请参见下文。

    static string SpaceLetters(string text, string separator)
    {
        return string.Join(separator, text.ToArray());
    } 

here is the solution: ASPX page: 解决方法如下:ASPX页面:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:TextBox ID="txtOriginalString" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtFormattedString" runat="server"></asp:TextBox>
    <asp:Button ID="btnFormat" Text="Format" runat="server" OnClick="btnFormat_Click" />
</asp:Content>

C# Code: C#代码:

protected void btnFormat_Click(object sender, EventArgs e)
{
    string formattedString = "";
    foreach (char c in this.txtOriginalString.Text.ToCharArray())
        formattedString += c + "-";
    this.txtFormattedString.Text = formattedString;
}

Is this what you wanted? 这就是你想要的吗?

var r = new Regex( @"([A-Za-z0-9])(?!$|\s)" );
var text = "Thisisatest";
var text2=r.Replace( text,"$1-" );

Another version: 另一个版本:

    string AddHyphens(string s)
    {
        string ret="";
        foreach(char c in s.ToCharArray())
            ret += ret != "" ? "-" + c : c.ToString();
        return ret;
    }
string str = "sometext";
string output="";
if(str.Length>0)
{
    output=""+str.ElementAt(0);

    for(int i =1; i< str.Length;i++)
        output += "-"+str.ElementAt(i); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM