string insertion ?
Hi all,
I want to insert "1" in the 0th position of string str. and i did it in this way..
string str="Val";
str.Insert(0,"1");
but its not working ?
it just shows "Val" any reason ?
regards,
rajs
[264 byte] By [
brraj] at [2007-11-19 10:44:45]

# 1 Re: string insertion ?
You have to catch the return value ;)
string str = "Val";
string newStr = str.Insert(0,"1");
# 2 Re: string insertion ?
Hi,
I did it in this way
string str = "Val";
str = str.Insert(0,"1");
and it works thanks torrud,
Do you know anyway to reverse a string with out using loop.
because i did not find any string reversing function.
cheers,
rajs
brraj at 2007-11-9 1:50:35 >

# 3 Re: string insertion ?
No, I do not know a method for reversing a string without doing it by a loop.
# 4 Re: string insertion ?
Ok, anyway thanks torrud
brraj at 2007-11-9 1:52:34 >

# 5 Re: string insertion ?
The Insert() takes the argument by value and returns a reference to the modified string. You need to get the returned value somewhere.
There is a StrReverse() method in VB to reverse a string. Havent come across any in C#.
Here you could use recursion. Havent tested the code but it goes somewhat like this:
public static string Reverse(string str)
{
if(1==str.Length)
{
return str;
}
else
{
return Reverse(str.Substring(1)) + str.Substring(0,1);
}
}
It doesn't use a loop. :)
Cheers,
Exterminator.
# 6 Re: string insertion ?
It doesn't use a loop. :)
That's right, but it use recursion and that is more expansive than a loop, because each call of the method reserve a block at the stack with the size of a full method call, that means it will saved the whole structure of a method call with all the parameters and so on. That is needed for finding the way back to the root if the deepest call is finished. So if you have a very huge string, you will maybe go out of memory. For short strings this way is ok, because it is lesser error-prone. Otherwise you have to implement a deep counter for preventing a stack overflow.
# 7 Re: string insertion ?
Hi torrud ,
I see a good point in your explanation
thanks
rajs
brraj at 2007-11-9 1:55:35 >

# 8 Re: string insertion ?
That's right, but it use recursion and that is more expansive than a loop, because each call of the method reserve a block at the stack with the size of a full method call, that means it will saved the whole structure of a method call with all the parameters and so on. That is needed for finding the way back to the root if the deepest call is finished. So if you have a very huge string, you will maybe go out of memory. For short strings this way is ok, because it is lesser error-prone. Otherwise you have to implement a deep counter for preventing a stack overflow.
I understand your point ..very correct...but the OP just wanted to know of some method other than the loop with help of which the aim is achieveable. I just tried pointing that to him.
Cheers.
# 9 Re: string insertion ?
Here is a solution without loops:
string txt = "Something you want to reverse";
Char[] chr = txt.ToCharArray()
Array.Reverse(chr)
string reversedText = new String(chr)
Of course this will involve memory allocation (on the stack only), but who cares about that ;-) right?
# 10 Re: string insertion ?
hi jhammer,
thanks for the solution
rajs
brraj at 2007-11-9 1:58:43 >
