adorer

help me please...



Re: Visual C# General how to write a function to read string (or byte)from right side

boban.s

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






Re: Visual C# General how to write a function to read string (or byte)from right side

adorer

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





Re: Visual C# General how to write a function to read string (or byte)from right side

boban.s

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






Re: Visual C# General how to write a function to read string (or byte)from right side

adorer

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





Re: Visual C# General how to write a function to read string (or byte)from right side

James Curran

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

 

 






Re: Visual C# General how to write a function to read string (or byte)from right side

adorer

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