how to write a function to read string (or byte)from right side

help me please...



Answer this question

how to write a function to read string (or byte)from right side

  • DanBog

    public static string ReverseStringWords(string input)
    {
    StringBuilder sb = new StringBuilder(input.Length);
    string[] words = input.Split(' ');
    for (int i = 0; i < words.Length - 1; i++)
    {
    sb.Insert(0, wordsIdea);
    sb.Insert(0,
    ' ');
    }
    sb.Insert(0, words[words.Length - 1]);
    return sb.ToString();
    }



  • R.Tutus

    If you mean on just Reverse characters in the string:
    public static string ReverseStringCharacters(string input)
    {
    StringBuilder sb = new StringBuilder(input.Length);
    foreach (char c in input)
    sb.Insert(0, c);

    return sb.ToString();
    }



  • Forch

    boban.s wrote:

    public static string ReverseStringWords(string input)
    {
    StringBuilder sb = new StringBuilder(input.Length);
    string[] words = input.Split(' ');
    for (int i = 0; i < words.Length - 1; i++)
    {
    sb.Insert(0, words);
    sb.Insert(0,
    ' ');
    }
    sb.Insert(0, words[words.Length - 1]);
    return sb.ToString();
    }

    thanks I will try it and if it is working ... it will solve a big problem for me


  • Rfreiberger

    boban.s wrote:

    If you mean on just Reverse characters in the string:
    public static string ReverseStringCharacters(string input)
    {
    StringBuilder sb = new StringBuilder(input.Length);
    foreach (char c in input)
    sb.Insert(0, c);

    return sb.ToString();
    }

    yes this is good but I want to reverse blocks of the string ... or bytes in better way

    for example ...

    1234 5678 0000 1111 ==>1111 0000 5678 1234


  • wycleft

    Well, if you are going to use a for() (as opposed to a foreach()), you might as well use it to your advantage:


    public static string ReverseStringWords(string input)
    {
       StringBuilder sb = new StringBuilder(input.Length);
       string[] words = input.Split(' ');
       for (int i = words.Length - 1; i >= 0; --i)
       {
     sb.Append(word[ i ]);
            sb.Append(' ');
       }
       if (sb.Length > 0)
           sb.Length --;   // remove final space.
       return sb.ToString();
    }

     

    Of course, as I was writting that, I thought of this method:


    public static string ReverseStringWords(string input)
    {
       string[] words = input.Split(' ');
       Array>Reverse(words);
       return String.Join(" ", words);
    }

     

     



  • sdoc

    James Curran wrote:

    Well, if you are going to use a for() (as opposed to a foreach()), you might as well use it to your advantage:


    public static string ReverseStringWords(string input)
    {
    StringBuilder sb = new StringBuilder(input.Length);
    string[] words = input.Split(' ');
    for (int i = words.Length - 1; i >= 0; --i)
    {
    sb.Append(word[ i ]);
    sb.Append(' ');
    }
    if (sb.Length > 0)
    sb.Length --; // remove final space.
    return sb.ToString();
    }

    Of course, as I was writting that, I thought of this method:


    public static string ReverseStringWords(string input)
    {
    string[] words = input.Split(' ');
    Array>Reverse(words);
    return String.Join(" ", words);
    }

    thanks ... the both are working but there is a hint here... :

    sb.Append(words[ i ]); in the first code not sb.Append(word[ i ]);

    and

    Array.reverse(words); in the second code not Array>Reverse(words);


  • how to write a function to read string (or byte)from right side