C# strings

Recommended Posts

ok sorry for asking so many questions bout this topic. but im having a real hard time with this. lets say i have a string _ret; and a char x; how can i insert x into _ret;

Share on other sites
ret.charAt(x) = char_var;
where x is the index you want to access. index being from 0 to size - 1

MSDN is a great resource and if you can't find it there, then just go to Google Romance [smile]. They match everyone up with what they need using great algorithms.

Share on other sites
ya the problem is that the char x is being assigned in a loop and I could just do
x.ToString then insert that string into _ret.
but since the x is beeing assigned in a loop it says, the use of "unnassigned local variable"
I dont get that. i mean its being assigned a value in the loop.

example.

public string function()
{
string _ret;int i=0;
while(i<1)
{
_ret="hello";
i=1;
}
return _ret;
}

it wont let me do that says _ret is an unassigned local variable.

Share on other sites
strings if i'm not confusing this with Java are reference variables.
so you have to do this:
public string function(){string _ret = new string(); //you may even have to put a size. //consult MSDN on initializing a string. //it's not the same as C++.int i=0;while(i<1){  _ret="hello";  i=1;}return _ret;}

Share on other sites
don't forget that strings are immutable, they can't be changed. Any operation you do to a string that "changes" it actually creates a whole new string. This can be slow if you are doing a lot of operations (like around 50 in one go). You might want to consider using a StringBuilder.

Share on other sites
Quote:
 Original post by Alpha_ProgDesstrings if i'm not confusing this with Java are reference variables.so you have to do this:*** Source Snippet Removed ***

huh I got an error with that it says no overload for method string.

Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by Alpha_ProgDesstrings if i'm not confusing this with Java are reference variables.so you have to do this:*** Source Snippet Removed ***

huh I got an error with that it says no overload for method string.

I think he was referring to the System.String object and not the string object. I have not used System.String though.

Generally in C# it is a good idea to give a string a default value. ie
string _s = "";
or
string _s = string.Empty;

Especially when you are doing operations that add onto the string. In fact most variables will be complained about if you modify them without giving them a value. Mostly because what value you expect the code below to give the integer?

int i;for(int k = 0; k < 10; k++){     i += k * i + 1;}

The problem is without initializing i, it could be anything. This is more true in C than in C# I believe... but it proves the point.

Share on other sites
Quote:
Original post by Krisc
Quote:
Original post by Anonymous Poster
Quote:
 Original post by Alpha_ProgDesstrings if i'm not confusing this with Java are reference variables.so you have to do this:*** Source Snippet Removed ***

huh I got an error with that it says no overload for method string.

I think he was referring to the System.String object and not the string object.

I didn't know there were 2 types of strings [shocked]

Share on other sites
Well, I do not believe they are different except for System.String might supply a Constructor where as string, much in the same way as int, double, etc. do not.

Share on other sites
Basically string is the same as System.String is just a name so we can use it more easily

It's just like a typedef in c++

In c# int stand for System.Int32
long for System.Int64

and yes in C# all the variables need to be initialized

Create an account

Register a new account

• Forum Statistics

• Total Topics
627733
• Total Posts
2978839

• 10
• 9
• 21
• 14
• 12