English 中文(简体)
• 如何将力量分离到果园
原标题: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;
}

因此,如果关键词要说,“Hello”。 我想这样做是为了产出/h/e/l/o,但此时此刻,它的唯一产出就是最后的特性。

谁能帮助?

最佳回答

on = vs += and String vs StringBuilder

你的问题是:

 kw = "/"+letters[i];

这是直截了当的转让,将超出以前的代号kw的价值。 或许请<代码>+=。 然而,此时,你们需要知道<代码>StringBuilder,以及为什么在 lo律中填写+=String

Related questions


On regular expressions

如果你重新学习正常的表达方式,你也可以这样做。 页: 1

References


Example

这里应当说明:

   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)"

一些关键想法:

  • 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

问题回答

或者,如果你使用4.0,你可以这样做:

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

使用

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

我试图通过与果园合作,优化利用这种记忆。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签