Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ex,
string str = "Hai";
Do line by line execution and check what str holds(Select the whole thing (cntl + A) and See after the letter i)
Posted
Comments
E.F. Nijboer 27-Jul-10 3:15am    
The question is not very clear? When writing to file for example you write one line at a time and the new line is added automatically. Do you have a code example?
koool.kabeer 27-Jul-10 3:17am    
If You want a White Space then press SpaceBar
OriginalGriff 27-Jul-10 3:18am    
I'm sorry, but your question(?) makes no sense. Please try to edit it and give more information on your problem. What are you seeing after the last letter in a string that shouldn't be there?
karthidew 28-Jul-10 0:50am    
Check out this code!!!

string str = "aaa";
str += "*";
string[] Ss = str.Split('*');
int a = Ss.Length;
Response.Write(a);
if(Ss[1] == string.Empty)
Response.Write("Empty string is there....");
DaveAuld 28-Jul-10 2:14am    
See my updated answer;

How can empty string be added to a a string?

if your string has something in it, then you can never see an empty string as part of the string.

Think you are miss understanding something.

In the code above if you stop the execution on the line, string str="Hai";, then it is possible that str = empty.string until after this line as "Hai" will not be assigned until you pass through this line.
if you add a breakpoint immediately after this line and look at the variables str will equal "Hai".


See the following code; it will test a string for String.Empty, and also for the position of the first known character 'a'; you will see that position positions come back the same. It is not a case of a String.empty being added anywhere, it is just how the system deals with 'nothing';

C#
string str = "aaa";
//Test 1
if (str.Contains(String.Empty))
    MessageBox.Show("String aaa contains an empty string");
//Test 2
int pos = str.IndexOf(String.Empty);
int pos2 = str.IndexOf("a");

//Feedback
MessageBox.Show("String.Empty at Position: " + pos.ToString());
MessageBox.Show("First 'a' is at Position: " + pos.ToString());
MessageBox.Show("The 2 position index match: " + (pos == pos2));
 
Share this answer
 
v2
Comments
karthidew 28-Jul-10 0:51am    
Check out this code!!!

string str = "aaa";
str += "*";
string[] Ss = str.Split('*');
int a = Ss.Length;
Response.Write(a);
if(Ss[1] == string.Empty)
Response.Write("Empty string is there....");
String.Empty isn't appended to the original string, what your seeing is the result of the split function recognising that the character you want to split by is at the end of the string.

If you don't want it to do this simply use the StringSplitOptions.RemoveEmptyEntries option.

C#
string str = "aaa"; 
str += "*"; 
string[] Ss = str.Split(new char[]{'*'}, StringSplitOptions.RemoveEmptyEntries);
int a = Ss.Length; 
Response.Write(a); 
if(Ss[1] == string.Empty) Response.Write("Empty string is there....");  


Of course, the last line will now throw an exception as Ss is only 1 element long.
 
Share this answer
 
v2
Comments
Sandeep Mewara 31-Jul-10 3:37am    
Comment from OP: Yes Only the split function creates an empty string when the splitting is done at the last position of the string object.
Thank You Martin...
raju melveetilpurayil 31-Jul-10 10:55am    
woow great... Thank You
Yes Only the split function creates an empty string when the splitting is done at the last position of the string object.
Thank You Martin...
 
Share this answer
 
Comments
Sandeep Mewara 31-Jul-10 3:38am    
Using comment feature to respond an answer will notify the person.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900