【问题标题】:How I replace a text like javascript in c# ...?如何在 c# 中替换诸如 javascript 之类的文本...?
【发布时间】:2017-06-15 12:28:05
【问题描述】:

我是 C# 编程的新手。我以前写过Javascript。我想像在 JavaScript 中一样替换 C# 中的文本。这是我的 JavaScript 代码:

var str = "this is my text";
str = str.replace(/\b(\w)/g,function(matched){
   return matched.toUpperCase();
});

这是如何在 C# 中完成的?在此先感谢,并为我糟糕的英语感到抱歉。

【问题讨论】:

标签: javascript c#


【解决方案1】:

你可以这样做:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string text = "this is my text";

        Regex rx = new Regex(@"\b(\w)");

        string result = rx.Replace(text, (Match m) => {
                return m.ToString().ToUpper().ToString();
        } );

        Console.WriteLine(result);// "This Is My Text"
        Console.ReadKey();
    }
}

【讨论】:

    【解决方案2】:

    在您的 JS 代码中,您希望将每个单词的首字母大写。在 c# 中,最好采用不同的方式。

    string lipsum1 = "this is my text";
    
    // Creates a TextInfo based on the "en-US" culture.
    TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
    
    // Changes a string to titlecase.
    Console.WriteLine("\"{0}\" to titlecase: {1}", 
                      lipsum1, 
                      textInfo.ToTitleCase( lipsum1 )); 
    
    // Will output: "this is my text" to titlecase: This Is My Text
    

    【讨论】:

      【解决方案3】:

      有几种情况可以替换字符串:

      1. 每个实例都被替换这很重要。 Replace 方法更改指定子字符串的每个实例。我们必须将结果分配给一个变量。

        using system;
        class Program
        {
           static void Main()
           {
           const string s = "Coding code is about writing Coding code the
                             right way";
           Console.WriteLine(s);
           string v = s.Replace("Coding", "Testing");
           Console.WriteLine(v);
           }
        }
        

      // 输入:编码代码是关于以正确的方式编写编码代码

      // 输出:测试代码是关于以正确的方式编写测试代码

      1. 仅替换单个字符串 将单词(和后面的空格)替换为新单词。我们分配给 Replace 方法的结果。

        using System;
        class Program
        {
          static void Main()
          {
            const string input = "key logger";
            Console.WriteLine("::BEFORE::");
            Console.WriteLine(input);
            string output = input.Replace("key ", "keyword ");
            Console.WriteLine("::AFTER::");
            Console.WriteLine(output);    
          }
        }
        

      // 输出:

      ::之前:: 键盘记录器

      ::之后:: 关键字记录器

      1. 使用 StringBuilder 替换字符串

        using System;
        using System.Text;
        
        class Program
        {
          static void Main()
          {
            const string s = "This is an example.";
        
            // Create new StringBuilder from string.
            StringBuilder b = new StringBuilder(s);
            Console.WriteLine(b);
        
            // Replace the first word.
            // ... The result doesn't need assignment.
            b.Replace("This", "Here");
            Console.WriteLine(b);
        
            // Insert the string at the beginning.
            b.Insert(0, "Sentence: ");
            Console.WriteLine(b);
          }
        }
        

      输出:

      这是一个例子。

      这是一个例子。

      句子:这是一个例子。

      【讨论】:

        【解决方案4】:

        您使用 String.Replace 方法 语法:

        String s = "This is an example.";
        s = s.replace("is","was");
        

        s 现在会说:这是一个例子。

        【讨论】:

        • 他的 JS 没有将 is 替换为 was。每个作品的首字母大写
        猜你喜欢
        • 2015-05-31
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2011-07-28
        • 2018-12-17
        • 1970-01-01
        相关资源
        最近更新 更多