2uzhan.com
Advertisement
Now Place:2uzhan.com » C# strings arrays

C# strings arrays

Software Development @ May 28, 2008   Views:0

Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

--------------Solutions-------------

Sorry, seems I didn't understand you well, you need to get last two characters and increment their value (means if input string xxxxAB so the result should be xxxxBC)?
if yes get last 2 characters get each of which increment thier ascii value by 1. that's my solution, if you need code reply again.

I've done done that my problem is that I cant preppend the previous characters in the buffer this is my solution:
--------------------
string input = "abcdef";
char[] mychar = input.ToCharArray();
int length = input.Length;
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++ )
sb.Append((char)((int)mychar + 2));
Console.WriteLine(sb.ToString());
Console.ReadLine();
--------------------
However this souliton iterates each character twice and I just want the last two

actually I figuree out but is there another shorter way to doi it :
string input = "mystring";
char[] mychar = input.ToCharArray();
int length = input.Length ;
string newinput = input.Substring(0, input.Length -2);
StringBuilder sb = new StringBuilder(length);
for (int i = length - 2; i < length; i++)
sb.Append((char)((int)mychar + 2));
Console.WriteLine(newinput + sb.ToString());

Hi,
Try following code.

class Program
{
    static void Main(string[] args)
    {
        string temp = "abcdef";
        char[] temp1 = new char[temp.Length];
        temp1 = temp.ToCharArray();
        for (int i = temp.Length-2;i<temp.Length;i++)
        {
            temp1[i] = Convert.ToChar((Convert.ToInt32(temp1[i]) + 2));
        }
        temp = new string(temp1);
    }
}

Finally the string " temp " contains the result you want.

Please feel free to ask me if you have any queries and let me know the code is working correctly or not.

Regards,
Vijay Bhaskar

Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

Write a java program that asks a person for his height (in meters) and weight (in kilograms) and
outputs one of the messages: underweight, normal, or obese, using the BMI ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...
Tags:
© 2018 2uzhan.com Contact