Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
In my project I want to remove last two digits if it is greater than nine and it has 11 or 12.

For Example,

30888293211 / 30888293212

I need "308882932"
Any one please help me on that.
Posted
Comments
BillWoodruff 29-Oct-13 9:02am    
I think you can help people trying to answer your question by stating what you are trying to do more clearly: something like

If a number has more than nine digits, and the last two digits of the number are either 11 or 12, then return a string that has the last two digits of the number removed.

If that's not correct, then please correct it.

See this: C# Remove string[^]

Regards..
 
Share this answer
 
Comments
Tom Marvolo Riddle 29-Oct-13 6:49am    
My 5!
Thanks7872 29-Oct-13 6:51am    
Thanks :-)
Try:
C#
string s = "30888293212";
if ((s.EndsWith("11") || s.EndsWith("12")) && s.Length > 9)
    s = s.Remove(9);
 
Share this answer
 
v2
Comments
WuRunZhe 29-Oct-13 6:37am    
Hi, ProgramFOX.
If input value is number not string, then how can your code works?
Thomas Daniels 29-Oct-13 6:38am    
Then first, you have to convert your long to a string, removing the digits, and converting the string to a long.
WuRunZhe 29-Oct-13 8:50am    
Oh, Two times converting? It's very unnecessary processing.

In my opinion, you have to first detect input variable's type and according to the type, process string with your suggestion and do number process (simple detect remainders and do sth). What about this?
Thomas Daniels 29-Oct-13 8:55am    
What do you mean exactly?
use this for get perfect result


string strVal = "30888293211";
strVal = strVal.Length > 9 ? (strVal.EndsWith("11") ? strVal.Substring(0, 9) : (strVal.EndsWith("12") ? strVal.Substring(0, 9) : strVal)) : strVal;
Response.Write(strVal + "");

strVal = "30888293212";
strVal = strVal.Length > 9 ? (strVal.EndsWith("11") ? strVal.Substring(0, 9) : (strVal.EndsWith("12") ? strVal.Substring(0, 9) : strVal)) : strVal;
Response.Write(strVal + "");

strVal = "30888293216";
strVal = strVal.Length > 9 ? (strVal.EndsWith("11") ? strVal.Substring(0, 9) : (strVal.EndsWith("12") ? strVal.Substring(0, 9) : strVal)) : strVal;
Response.Write(strVal + "");
 
Share this answer
 
v2
Hi.
In my project I want to remove last two digits if it is greater than nine and it has 11 or 12.

At there, what the "nine" means?

I realized like this.
First: "Nine" means every last two digits's each value. For example if numerical string is "23328391829", last two digits 2 and 9's each value is greater than nine. If it so, number can be 0~9 and there is no other number greater than 9.

Second, "Nine" means last two digits's value. For example, if numerical string is "34234293819", last two digits's value is "19". If this value is greater than 9, you have to remove this. If it is so, there is no need to say that "..... it has 11 or 12". Because 11 and 12 is also greater than 9.

Among them, which one is your destination?

I can solve this problem under your detailed answers. Thank you.
 
Share this answer
 
Comments
Thomas Daniels 29-Oct-13 6:39am    
This is not an answer, but a comment asking for explanation. So, please don't post this as an answer to the question, but click on the "Have a Question or Comment?" button under the question, and then you can post your comment.
Member gok 29-Oct-13 6:59am    
Sorry for the miscommunication.

I wants to remove last 2 digits, if it is ends with 11 or 12.

Thanks
WuRunZhe 29-Oct-13 8:53am    
Oh, really, sorry. Thank you for your advice.
C#
private long trimLastTwoDigits(object objValue)
{
      long lRet = 0;
      if (objValue is String)
      {
           if (long.TryParse(objValue as String, out lRet) == false)
           return -1;
      }

      if (lRet % 100 == 11 || lRet % 100 == 12)
           lRet = lRet / 100;

       return lRet;
}

Well, you can get number which you want.

If you want to process your input value with string, see below.
C#
private string trimLastTwoDigits(object objValue)
{
    string str = objValue as String;
    int nLen = str.Length;

    str = (str.EndsWith("11") || str.EndsWith("12")) ? str.Substring(0, nLen - 2) : str;

    return str;
}
 
Share this answer
 

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