Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A variable contains a string value. i need to take the last two characters of that string. can i use the substring function for this purpose?if so can any one give me a simple example for that.
or is there another way of doing this?
Posted

Yes you can use the substring function. See here[^] for an example.
 
Share this answer
 
Comments
RakeshMeena 17-Jun-11 0:15am    
Correct! My 5
walterhevedeich 17-Jun-11 0:33am    
Thanks Rakesh
Sergey Alexandrovich Kryukov 17-Jun-11 0:51am    
Correct, my 5.
Now, please take a look at my solution.
It is certainly less relevant but I'm sure it's more practical.
--SA
To reply to your question, I've done the following: I counted total number of public members of all public types of just two most used standard .NET libraries: "mscorelib" and "System.Core", both covering the name space System. I did not count the types, only the type members, such as System.String.Substring.

Here are the results:

    <il>mscorelib: 39882 public members
    <il>System.Core: 6811 public members
    <il>Total: 46693 public members


These two assemblies are most used in all .NET development. Let's make the most conservative assumption: you're going to use only 10% of all members and only 10% of the methods you're going to use are as "non-trivial" as System.String.Substring, so they potentially may cause you to ask your CodeProject questions. Effectively, it means you will feel a need to ask your questions only about 1% of the members.

It will give an estimate of some 450 CodeProject questions (after rounding in the direction of reduction). It does not seem practical. Don't you thing there is something wrong with your method of learning?

Did you notice that Microsoft provides a MSDN help page in each and every public member of each type?

APPENDIX:
Is someone is curious, here is how I did the calculations:

using System;
using System.Reflection;

//...

static int CountPublicMembers(Assembly assembly) {
    int result = 0;
    Type[] types = assembly.GetTypes();
    foreach (Type type in types)
        result += type.GetMembers().Length;
    return result;
} //CountPublicMembers

static void CountAll() {
    Assembly mscorelib = typeof(System.String).Assembly;
    Assembly systemCore = typeof(System.Action).Assembly;
    int iMscorelib = CountPublicMembers(mscorelib);
    int iSystemCore = CountPublicMembers(systemCore);
    System.Console.WriteLine(
        "{0}+{1} = {2}",
        iMscorelib, iSystemCore,
        iMscorelib + iSystemCore);
} //CountAll


—SA
 
Share this answer
 
v3
Try
string str = "YourStringValues";
  str = str.Substring(str.Length - 2); // Passing parameter Zero based starting character position of substring.
Console.WriteLine(str);  // Print Last two Character eg:- es

output :- es
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jun-11 0:54am    
Correct, my 5.
Now, please take a look at my solution.
It is certainly less relevant but I'm sure it's more general and practical.
--SA
NDebata 17-Jun-11 2:26am    
It will not work for a string having length < 2
C#
string str = "Hello";
string lasttwo = str.Substring(str.Length > 1 ? str.Length - 2 : 0);
 
Share this answer
 
TextBox1.Text.ToString().Substring (0,30);

u can use it like this.
 
Share this answer
 
Comments
Per Söderlund 17-Jun-11 1:34am    
No need to convert text ToString().
TextBox1.Text is already a string.
use
C#
string str ="sanjeev";

string res=str.Substring(str.Length - 2); 


in res is "ev"
 
Share this answer
 
Comments
Linto Leo Tom 4-Jun-12 6:14am    
The exact solution the questioner wants... +5

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