问题描述
如何分隔数字如何获取'\'之前的数字以及单独斜线之后 i这样做
< span class =code-keyword> private String removeCodeNumber( String covCode) { 字符串 [] cov = covCode.Split(' / ); if (cov.Length > 2 ) { return cov [ 0 ]。ToString( ); } else { return covCode; } } 私有 字符串 removeCovcode( String codenbr) { String [] cov = codenbr.Split(' /'); if (cov.Length > 2 ) { return cov [ 1 ]。ToString( ); } else { return codenbr; } }
例子covcode有161 \ 47899 现在我需要分开这两个值
解决方案嘿那里, 在
拆分
方法中尝试 \\
: covCode.Split(' \\')
如果有帮助请告诉我。 Azee ...
你的代码应该可以工作。 你的问题是斜线的方向吗?您的书面解释是使用反斜杠但您的代码使用正斜杠?
在两种方法中,您都要检查
Split()的结果是否
大于 2项。 (我也假设你想要你在文本中隐含的反斜杠你的问题。) 尝试: private 字符串 removeCodeNumber(字符串 covCode) { / / 在这种情况下,不需要检查结果长度! // 如果未找到搜索字符,则返回仅包含covCode的数组。 return covCode.Split(' \\')[ 0 ]; } private 字符串 removeCovcode( String codenbr) { String [] cov = codenbr.Split(' \\'); if (cov.Length > = 2 ) { return cov [ 1 ]; // 。ToString();是不必要的,它已经是一个字符串 } return codenbr; // 其他是不必要的,因为上面的返回会阻止fall-thru }
how to seperate the digits how to get the digits before '\' and after slash seperately i did like this
private String removeCodeNumber(String covCode)
{
String[] cov=covCode.Split('/');
if (cov.Length > 2)
{
return cov[0].ToString();
}
else
{
return covCode;
}
}
private String removeCovcode(String codenbr)
{
String[] cov = codenbr.Split('/');
if (cov.Length > 2)
{
return cov[1].ToString();
}
else
{
return codenbr;
}
}
example covcode is having 161\47899 now i need sepereate these two values
解决方案 Hey there, Try\\
in the Split
method:
covCode.Split('\\')
Let me know if it helps. Azee...
Your code should work. Is your problem the direction of your slash? Your written explanation is using back-slash but your code is using forward-slash?In both methods you are checking if the result of theSplit()
has greater than 2 items.
(I'm also assuming that you wanted the backslash as you implied in the text of your question.)
Try:
private String removeCodeNumber(String covCode)
{
// in this case no checking of the result length is necessary!
// if the search character isn't found then an array containing only the covCode is returned.
return covCode.Split('\\')[0];
}
private String removeCovcode(String codenbr)
{
String[] cov = codenbr.Split('\\');
if (cov.Length >= 2)
{
return cov[1]; //.ToString(); is unnecessary, it's already a string
}
return codenbr; // else is unnecessary because above return prevents "fall-thru"
}