简体   繁体   English

如何将String分成chars

[英]How to separate String into chars

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". 因此,如果关键字是“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 我希望这个输出/ h / e / l / l / o但是目前它只输出最后一个字符,在这种情况下/ o

Can someone help? 有人可以帮忙吗?

on = vs += and String vs StringBuilder on = vs +=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. 这是一个直接赋值,将覆盖上一次迭代的kw值。 Perhaps you want += . 也许你想要+= However, at this point you need to know about StringBuilder and why doing += with String in a loop yields bad performance. 但是,此时您需要了解StringBuilder以及为什么在循环中执行+= with String导致性能不佳。

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 . 您只需匹配每个字符x并将其替换为/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 除非您需要显式索引,否则请使用foreach循环
  • When building a string in a loop, use StringBuilder 在循环中构建字符串时,请使用StringBuilder
  • When properly used, regular expressions are great! 正确使用时,正则表达式很棒!

References 参考

Attachments 附件

Or, if you use .NET 4.0, you can do this: 或者,如果您使用.NET 4.0,则可以执行以下操作:

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

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;
}

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);
}

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

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