如何将字符串分割成字符

发布于 2024-09-07 04:29:10 字数 428 浏览 3 评论 0原文

public static string kw;

public String parse(String keyword)
{
    this.keyword = keyword;
    char[] letters = keyword.ToCharArray();
    string g;

    long length = System.Convert.ToInt64(keyword.Length.ToString());
    for (int i = 0; i <= length-1; i++)
    {
        kw = "/"+letters[i];
    }
    return kw;
}

因此,如果关键字是“Hello”。我希望输出 /h/e/l/l/o 但目前它只输出最后一个字符,在这种情况下 /o

有人可以帮忙吗?

public static string kw;

public String parse(String keyword)
{
    this.keyword = keyword;
    char[] letters = keyword.ToCharArray();
    string g;

    long length = System.Convert.ToInt64(keyword.Length.ToString());
    for (int i = 0; i <= length-1; i++)
    {
        kw = "/"+letters[i];
    }
    return kw;
}

So if the keyword is lets say, "Hello". I want this to output /h/e/l/l/o but at the moment its only outputting the last character, in this case /o

Can someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人生戏 2024-09-14 04:29:10

=+=StringStringBuilder

上您的问题在于这一行:

 kw = "/"+letters[i];

这是一个直接赋值,并将覆盖上一次迭代中的 kw 值。也许您想要+=。但是,此时您需要了解 StringBuilder 以及为什么在循环中使用 String 执行 += 会产生糟糕的性能。

相关问题


关于正则表达式

如果您准备学习正则表达式,您也可以用一行来完成此操作。您只需匹配每个字符 x 并将其替换为 /x 即可。

参考文献


示例

下面是一个应该具有说明性的代码片段:

   string keyword = "hello";

   foreach (char ch in keyword) {
      Console.Write("[" + ch + "]");
   }
   Console.WriteLine();
   // prints "[h][e][l][l][o]"

   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < keyword.Length; i++) {
      sb.Append("<" + keyword[i] + ">");
   }
   Console.WriteLine(sb);
   // prints "<h><e><l><l><o>"

   Console.WriteLine(new Regex(@"(?=.)").Replace(keyword, @"/"));
   // prints "/h/e/l/l/o"

   Console.WriteLine(new Regex(@"(.)").Replace(keyword, @"($1$1)"));
   // prints "(hh)(ee)(ll)(ll)(oo)"

一些关键思想:

  • 除非需要显式索引,否则使用 foreach 循环
  • 在循环中构建字符串时,请使用 StringBuilder
  • 当使用得当,正则表达式很棒!

参考资料

附件

on = vs += and String vs StringBuilder

Your problem is in this line:

 kw = "/"+letters[i];

This is a straight assignment, and will overwrite the value of kw from the previous iteration. Perhaps you want +=. However, at this point you need to know about StringBuilder and why doing += with String in a loop yields bad performance.

Related questions


On regular expressions

If you're up to learning regular expression, you can also do this with one line. You simply match each character x and replace it with /x.

References


Example

Here's a snippet that should be illustrative:

   string keyword = "hello";

   foreach (char ch in keyword) {
      Console.Write("[" + ch + "]");
   }
   Console.WriteLine();
   // prints "[h][e][l][l][o]"

   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < keyword.Length; i++) {
      sb.Append("<" + keyword[i] + ">");
   }
   Console.WriteLine(sb);
   // prints "<h><e><l><l><o>"

   Console.WriteLine(new Regex(@"(?=.)").Replace(keyword, @"/"));
   // prints "/h/e/l/l/o"

   Console.WriteLine(new Regex(@"(.)").Replace(keyword, @"($1$1)"));
   // prints "(hh)(ee)(ll)(ll)(oo)"

Some key ideas:

  • Unless you need explicit index, use foreach loop
  • When building a string in a loop, use StringBuilder
  • When properly used, regular expressions are great!

References

Attachments

情话难免假 2024-09-14 04:29:10

或者,如果您使用 .NET 4.0,则可以执行以下操作:

string someString = "abc";
string result = string.Join("/", (IEnumerable<char>)someString);

Or, if you use .NET 4.0, you can do this:

string someString = "abc";
string result = string.Join("/", (IEnumerable<char>)someString);
骑趴 2024-09-14 04:29:10

用这个

public String parse(String keyword)
{
    if (string.IsNullOrEmpty(keyword))
        return string.Empty;

    var retVal = (from v in keyword.ToArray()
                    select v.ToString())
                    .Aggregate((a, b) => a + "/" +b);

    return retVal;
}

Use this

public String parse(String keyword)
{
    if (string.IsNullOrEmpty(keyword))
        return string.Empty;

    var retVal = (from v in keyword.ToArray()
                    select v.ToString())
                    .Aggregate((a, b) => a + "/" +b);

    return retVal;
}
无人问我粥可暖 2024-09-14 04:29:10

我尝试通过使用字符来优化它以使用更少的内存。

public string Parse(string input)
{
    char[] arrResult = new char[input.Length*2];
    int i = 0;
    Array.ForEach<char>(input.ToCharArray(), delegate(char c)
                                                {
                                                    arrResult[i++] = '/';
                                                    arrResult[i++] = c;
                                                });
    return new string(arrResult);
}

I tried to optimize this to use less memory by working with chars.

public string Parse(string input)
{
    char[] arrResult = new char[input.Length*2];
    int i = 0;
    Array.ForEach<char>(input.ToCharArray(), delegate(char c)
                                                {
                                                    arrResult[i++] = '/';
                                                    arrResult[i++] = c;
                                                });
    return new string(arrResult);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文