Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
i want to get this string last 4 character
string is Formandmanner.AAA
expectations-- .AAA in C#
Posted
Comments
hitech_s 30-Mar-12 6:21am    
is dot(.) presents always, if so, we can split it like this

string s="Formandmanner.AAA";
string data=s.split('.')[1]

output :: .AAA

Hello

C#
string text = "abcdefghijklmn";

string myString = (text.Length > 3)? text.Substring(text.Length - 4, 4): text;

MessageBox.Show(myString);
 
Share this answer
 
Comments
VJ Reddy 1-May-12 20:25pm    
Good answer. 5!
I think there is an overload to get the last part without mentioning length as lentext.Substring(text.Length - 4) instead of text.Substring(text.Length - 4, 4)
Shahin Khorshidnia 2-May-12 5:49am    
Thank you very much VJ.
I think so, but I had not the presence of mind at that time :D
jonlink01 30-May-13 4:19am    
good ans 5!
Shahin Khorshidnia 30-May-13 10:42am    
Thank you very much :)
try:
C#
string temp = "Formandmanner.AAA";
string output = temp.Substring(temp.Length - 4, 4);

output will have .AAA value
 
Share this answer
 
You can also write an extension method for this

C#
public static string Right(this string s, int length)
        {
            length = Math.Max(length, 0);

            if (s.Length > length)
            {
                return s.Substring(s.Length - length, length);
            }
            else
            {
                return s;
            }
        }


And then you can do

C#
var result = "Formandmanner.AAA".Right(4);
 
Share this answer
 
Comments
fjdiewornncalwe 30-Mar-12 16:30pm    
+5. Very nice and reusable solution.
Bit2 Developer 2-Dec-15 2:17am    
How to pick last two digits of current year.
Look here[^] Check out the Substring methods.

If this is not what you need please clarify the question.
Hope this helps.
 
Share this answer
 
Comments
ganeshbdas 30-Mar-12 5:53am    
same can u help me with Example
V. 30-Mar-12 6:02am    
Which proves you haven't checked out the link very well as there was an example there. Please investigate issues yourself more thouroughly before posting here.
Shahin Khorshidnia 30-Mar-12 6:13am    
My +5
V. the link is great but some OPs are not to feel in the mood for refering. They want ready solutions to do their job ;) If you don't prepare one, then he will call to every one who he knows! So please save his friends!! :D
hey try this
string s="Formandmanner.AAA";
string result = s.Substring(s.Length - 4);

best luck
 
Share this answer
 
v2
Comments
Rajeev Jayaram 30-Mar-12 6:30am    
[Edit] - Added pre tag for better readability.
This psedo code may help you.You can use substring also

for (int i =0 ; i < struingtext.length ; i++)
{
   int pos  = stre.length-4;
   if ( i >= pos)
   {
     console.writeline(struingtext[i])

   }

}
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 30-Mar-12 6:07am    
1. lenght(Lenght) console(Console) writeline(WriteLine). C# is a C family language and it's case sensitive.
2. Doesn't need "for"
3. The most important is the code doesn't work!
Rajeev Jayaram 30-Mar-12 6:32am    
[Edit] - Added pre tag for better readability.

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