Click here to Skip to main content
16,016,394 members
Please Sign up or sign in to vote.
2.21/5 (6 votes)
See more:
In c# , i am creating a calculator. Now, i just read one of the forum here and got this code for "backspace" button. Now, its working for me.

I want to know whats the meaning of
C#
textBox1.Text.Substring(0, i - 1)

int i = textBox1.Text.Length;
           textBox1.Text = textBox1.Text.Substring(0, i - 1);
Posted
Updated 5-Mar-14 2:29am
v4

C#
string input = "abcdefghi";

    // Get first three characters
    string sub = input.Substring(0, 3);
    Console.WriteLine("Substring: {0}", sub);
    }
}

Output

Substring: abc
 
Share this answer
 
C#
string input = "abcdef";

    // Get first three characters
    string sub = input.Substring(0, 3);

result is abc
 
Share this answer
 
C#
int i = textBox1.Text.Length;
textBox1.Text = textBox1.Text.Substring(0, i - 1);


In your example Substring(0, i - 1) means,Start from the 0th index and total text length goes for the (i-1).

LIVE DEMO : .Net Fiddle

Please Read for more : C# Substring

UPDATE

Below code snippet gets first three characters of the 'input' string.It's like this.It starts from the 0th index and total length of the final output should be 3 characters long.

C#
string input = "OneTwoThree";

// Get first three characters
string sub = input.Substring(0, 3);


OUTPUT
One
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 10:57am    
When dealing with range, it's good to explain which indices are inclusive or not... if OP is so naive... :-) voted 4.
—SA
Sampath Lokuge 6-Mar-14 1:29am    
Thanks Sergey.I have updated my answer. :)
SubString returns a part of the string, by either

specified starting character position --> the end of the string
C#
string a = "Hello World";
string b = a.SubString(6);
Console.WriteLine(b);

output:
World

specified starting character position --> a specified lenght
C#
string a = "Hello World";
string b = a.SubString(0,5)
Console.WriteLine(b);

output:
Hello

http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 10:53am    
Answered. A 5.
—SA
george4986 5-Mar-14 23:22pm    
+5v ;-)
Thomas Daniels 8-Mar-14 5:42am    
Good answer, +5, but I suggest a minor change: change to a specified ending character position into with a specified length because you specify a length, not a position.
Paddy84 10-Mar-14 9:36am    
Good catch.
Thanks
Paddy
Consider a small example of mine, so that you will be able to relate with your code.

String MyEmail = "SanjuBollera@gmail.com";
int HowManyChracters = 5;
int FromWhatPosition = 13;
Console.WriteLine(MyEmail.Substring(FromWhatPosition)); //This returns a SUBSTRING "gmail.com"

Console.WriteLine(MyEmail.Substring(FromWhatPosition, HowManyChracters)); //The output is "gmail"

//The first parameter tells you from where should you start extracting the string
//The second parameter tells you how many characters you should extract from that substring.
 
Share this answer
 
Comments
phil.o 5-Mar-14 8:51am    
You really should avoid making your email address appear on public forums; unless you do not mind being spammed to death one day or another :)
substring is a method of a string it returns only the value between the range

C#
string input = "OneTwoThree";

   
    string sub = input.Substring(0, 3);
    Console.WriteLine("Substring: {0}", sub);



Result:
one
C#
string input = "OneTwoThree";

   
    string sub = input.Substring(4, 3);
    Console.WriteLine("Substring: {0}", sub);


Result:
Two
C#
substring('yourstring',startIndex,endIndex)



More Info
 
Share this answer
 
v5
Comments
BillWoodruff 5-Mar-14 19:41pm    
The second example is incorrect: input.Substring(3, 6) will return six characters.
King Fisher 5-Mar-14 23:16pm    
yes your right..thanks
gggustafson 2-Apr-14 17:45pm    
Please fix your example to specify "leng of returned substring".
We saw many examples concentrated on the Substring instance method with one or two arguments. With MSDN, we researched the Substring method. Finally we learned about Slice, Substring exceptions, and Substring performance.


Review:
Substring is useful.
It can help simplify your programs.
It has no significant performance problems
What is SubString

Thanks,
_RG
 
Share this answer
 
You should take the habit to go and see MSDN when it comes to using basic .NET objects (in that case, the String object):
String.Substring Method[^]

It will explain everything you should know about the Substring() method and its overloads.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 10:52am    
Golden words, a 5. :-)
—SA
phil.o 5-Mar-14 10:53am    
Thank you Sergey :)

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