我试图从字符串的整数部分删除前0。
如果"CATI-09100"
在0
部分中interger
首先删除它,则字符串将为"CATI-9100"
。否则没有变化。我尝试使用substring
。但我需要更好,更有效的方法来做到这一点。此外,"CATI-"
将出现在每个字符串中。任何提示都可以。
我正在思考以下几点:
strICTOID = Convert.ToString(drData.GetValue(0).Equals(System.DBNull.Value) ? string.Empty : drData.GetValue(0).ToString().Trim());
if (strICTOID.Length > 0)
{
indexICTO = strICTOID.IndexOf("-");
}
答案 0 :(得分:2)
使用简单的字符串替换。
string text = "CATI-09100";
string newText = text.Replace("-0", "-"); // CATI-9100
答案 1 :(得分:2)
你可以使用这样的东西 -
string check = indexICTO.Split('-')[1]; // will split by "-"
if(check[0].Equals("0")) // will check if the charcter after "-" is 0 or not
indexICTO = indexICTO.Replace("-0", "-");
答案 2 :(得分:1)
如果要在整数开头删除所有零,可以执行以下操作:
your_string = Regex.Replace(your_string, @"-0+(\d+)", "$1");
//CATI-009100 --> CATI-9100
//CATI-09100 --> CATI-9100
//CATI-9100 --> CATI-9100
答案 3 :(得分:0)
引用Replace first occurrence of pattern in a string
var regex = new Regex(Regex.Escape("0"));
var newText = regex.Replace("CATI-09100", "", 1);
答案 4 :(得分:0)
这是一种新手,但肯定有效。
string myString , firstPart,secondPart ;
int firstNumValue;
myString = "CATI-09994";
string[] parts = myString.Split('-');
firstPart = parts[0];
secondPart = parts[1];
firstNumValue = int.Parse(secondPart.Substring(0, 1));
if(firstNumValue == 0){
secondPart = secondPart.Remove(0,1);
}
Console.WriteLine(firstPart+"-"+secondPart);