Je tente d'analyser une chaîne de quelque chose comme ceci:Chaîne Parsing en Java
entrée « 20:00 » - sortie serait « 20 » entrée « 02:30 » - sortie serait « 2 » Entrée "00:30" - la sortie serait "".
Voici comment j'ai écrit, je n'aime pas la façon dont je le fais, à la recherche d'une façon plus efficace de le faire en un seul balayage. Des idées?
private String getString(final String inputString)
{
String inputString = "20:00"; // This is just for example
final String[] splittedString = inputString.split(":");
final String firstString = splittedString[0];
int i;
for (i = 0; i < firstString.length(); i++)
{
if (firstString.charAt(i) != '0')
{
break;
}
}
String outputString = "";
if (i != firstString.length())
{
outputString = firstString.substring(i, firstString.length());
}
return outputString;
}
'Chaîne v = Integer.toString (Integer.parseInt (input.split (": ") [0])); return v.equals ("0")? "": v; ' – OscarRyz
'! "00" .equals ("0") ' –
ce que je veux aussi autoriser" 2:30 "espaces en entrée. Dans un tel scénario, je crois que Interger.parseInt va exploser. – Rishi