iT邦幫忙

0

kotlin 字符擷取後替換

  • 分享至 

  • twitterImage

https://ithelp.ithome.com.tw/upload/images/20191210/20123448eLli2lNsAf.png
請問如何replace?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

5
海綿寶寶
iT邦大神 1 級 ‧ 2019-12-10 20:25:32
最佳解答

舉例
oldt="0123456789"
startnub=3
nums=3
newt="abc"
以下的做法是「將以下三段字串接起來成為新字串」
1.oldt 從一開始到 startnub,即 "012"
2.newt,即 "abc"
3.oldt 從 startnub+nums 到 結尾,即 "56789"
得到結果為 "012abc56789"

/**
 * You can edit, run, and share this code. 
 * play.kotlinlang.org 
 */
import kotlin.math.pow

fun main() {
    var workstring = "天無絶人之路"
    println(replace(workstring, 2, 2, "肥而不膩"))
}

fun replace(oldt:String, startnub:Int, nums:Int, newt:String) : String {
    return oldt.substring(0, startnub) + newt + oldt.substring(startnub+nums)
}

選我最佳解答

看更多先前的回應...收起先前的回應...
gt0415 iT邦新手 5 級 ‧ 2019-12-10 20:35:42 檢舉

感謝

感覺題目像海綿寶寶出的一樣.因為實在看不懂發問者表達的,那個
函數居然是叫replace,但又不是一般replace函數的功能,就問
了個如何replace.居然還能補出這樣多東西.
跟幾年前看到尼克問得這題一樣
https://ithelp.ithome.com.tw/questions/10188641

那句 如何SUM()函式,我是需要做GROUP BY 的相同類型做數值相乘。
實在是不好理解,然後暐翰就能互動的很好的解答,好像兩個人就坐在旁邊一樣.真是有趣.

小魚 iT邦大師 1 級 ‧ 2019-12-11 07:59:53 檢舉

哇哈哈,
那已經很久了,
不過我後來發現人家也是真的有料的。

我要發表回答

立即登入回答