string carPrice = driver.FindElement(By.XPath("//body/main[1]/article[1]/section[1]/section[1]/section[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/h3[1]")).Text;
string basePrice = "USD1000";
carPrice 是动态值,包含货币和逗号,例如 USD42,000
答案 0 :(得分:0)
如果您的定价格式是固定的,您可以使用此 //USD42,000
char[] delimiterChars = {',','U','S','D'};
string[] values =carPrice.Split(delimiterChars);
string join = string.Join("", values);
int price = int.parse(join);
答案 1 :(得分:0)
int price = int.parse(join);
以上应该是:
int price = int.Parse(join);
Parse 只是一个简单的大写 P。
答案 2 :(得分:0)
这里有几种语言扩展方法可以提供帮助,忽略数组扩展,我已经有几个这样的方法了。
$objectToArray
单元测试
public static class Extensions
{
/// <summary>
/// Convert string to decimal
/// </summary>
/// <param name="sender">assumed value has one or more digest</param>
/// <returns>decimal value or will throw an exception if can not convert e.g. an empty string</returns>
public static decimal ToDecimal(this string sender) =>
decimal.Parse(Regex.Replace(sender, @"[^\d.]", ""));
/// <summary>
/// Any value in array greater than checkValue
/// </summary>
/// <param name="sender">valid decimal array</param>
/// <param name="checkValue">check if an element in sender is greater than this value</param>
/// <returns>true if condition is met, false if not met</returns>
public static bool GreaterThan(this decimal[] sender, decimal checkValue) =>
sender.Any(value => value > checkValue);
/// <summary>
/// Is sender greater than checkValue
/// </summary>
/// <param name="sender">valid decimal value</param>
/// <param name="checkValue">is sender greater than this value</param>
/// <returns>true if condition is met, false if not met</returns>
public static bool GreaterThan(this decimal sender, decimal checkValue) =>
sender > checkValue;
public static decimal[] ToDecimalArray(this string[] sender)
{
var decimalArray = Array
.ConvertAll(sender,
(input) => new
{
IsDecimal = decimal.TryParse(Regex.Replace(input, @"[^\d.]", ""), out var doubleValue),
Value = doubleValue
})
.Where(result => result.IsDecimal)
.Select(result => result.Value)
.ToArray();
return decimalArray;
}
}