[英]string split and setting based on count in java
我有一個像
String delay = "10:20:30";
int Retrycount=3;
long DelayTime=0;
我必須根據int的計數值在DelayTime
進行拆分和設置。
例如,如果Retrycount為1,則該值應為10。延遲值可能會像“ 10:20:30:40:50”一樣增加
如何根據計數設置值?
您想要的似乎是兩件事:
最簡單的實現是這樣的:
public String splitAndPick(String splitMe, int pickMe) { String[] splitString = splitMe.split(":"); return splitString[pickMe - 1]; }
然后,您可以像這樣設置延遲時間:
long delayTime = splitAndPick(delay, retryCount);
另外,我假設您正在做作業:在Java中,您以小寫字母開頭的變量。 如果您有多個單詞,則將每個單詞的第一個字母作為第一個大寫字母。 例如:延遲,而不是延遲。 retryCount,而不是Retrycount。 delayTime,而不是DelayTime。
此約定使其他程序員在執行操作時更容易理解您的意思。
有關示例 ,請參見Sun的舊 樣式指南和Google的樣式指南 。 老實說,我建議您堅持使用Google的指南,但Sun的舊指南主要是針對歷史背景,盡管它們對於您的需求差異不大。
試試這個解決方案。
String delay = "10:20:30";
int Retrycount=3;
long DelayTime=0;
//split function with 2 argument will not split the string further, as in our case we need to split the string till Retrycount
String[] values = delay.split( ":", Retrycount+1 );
//fetch the element based on index , index starts from 0 , so we need to subtract one from "Retrycount"
System.out.print( values[Retrycount-1] );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.