好吧,我有3个看起来像这样的字符串
String c = "coins<col=ffffff> x <col=ffff00>";
String c2 = "coins<col=ffffff> x <col=ffffff>100k (100,000)";
String c3 = "coins<col=ffffff> x <col=00ff80>10m (10,000,000)";
对于字符串“ c”,我使用了:
Integer.parseInt(i.getMessage().toLowerCase().replace(c, "").replace(",", ""));
问题是字符串c2和c3有所不同。
我正在努力得到这个
整数c2 = 100000;
int c3 = 10000000;
请帮忙!
没有正则表达式也一样容易。
public class Stripper
{
public static void main (String[] args)
{
String c = "coins<col=ffffff> x <col=ffff00>";
String c2 = "coins<col=ffffff> x <col=ffffff>100k (100,000)";
String c3 = "coins<col=ffffff> x <col=00ff80>10m (10,000,000)";
String stripped = strip (c);
if (!stripped.isEmpty ())
System.out.println (Integer.parseInt (stripped));
String stripped2 = strip (c2);
if (!stripped2.isEmpty ())
System.out.println (Integer.parseInt (stripped2));
String stripped3 = strip (c3);
if (!stripped3.isEmpty ())
System.out.println (Integer.parseInt (stripped3));
}
private static String strip (String text)
{
int first = text.indexOf ('(');
if (first < 0)
return "";
int last = text.indexOf (')', first);
if (last < 0)
return "";
return text.substring (first + 1, last).replaceAll (",", "");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句