Splitting a String into only 2 parts

前端 未结 5 1112
抹茶落季
抹茶落季 2021-02-07 21:52

I want to take a string from a textbox (txtFrom) and save the first word and save whatever is left in another part. (the whatever is left is everything past the first space)

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 22:35

    You can also try RegularExpressions

    Match M = System.Text.RegularExpressions.Regex.Match(source,"(.*?)\s(.*)");
    M.Groups[1] //Bob
    M.Groups[2] // jones went to the store
    

    The regular expression matches everything up to the first space and stores it in the first group the ? mark tells it to make the smallest match possible. The second clause grabs everything after the space and stores it in the second group

提交回复
热议问题