Click here to Skip to main content
12,625,619 members (34,207 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
i want to get this string last 4 character
string is Formandmanner.AAA
expectations-- .AAA in C#
Posted 30-Mar-12 0:42am
Comments
hitech_ssc 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hello

string text = "abcdefghijklmn";
 
string myString = (text.Length > 3)? text.Substring(text.Length - 4, 4): text;
 
MessageBox.Show(myString);
  Permalink  
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 :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

try:
string temp = "Formandmanner.AAA";
string output = temp.Substring(temp.Length - 4, 4);
output will have .AAA value
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

You can also write an extension method for this

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

var result = "Formandmanner.AAA".Right(4);
  Permalink  
Comments
Marcus Kramer 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Look here[^] Check out the Substring methods.

If this is not what you need please clarify the question.
Hope this helps.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

hey try this
string s="Formandmanner.AAA";
string result = s.Substring(s.Length - 4);
best luck
  Permalink  
v2
Comments
Rajeev Jayaram 30-Mar-12 6:30am
   
[Edit] - Added pre tag for better readability.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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])
 
   }
 
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.1 | Last Updated 30 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100