Best way to split string by last occurrence of character?

前端 未结 4 857
情话喂你
情话喂你 2020-12-28 11:49

Let\'s say I need to split string like this:

Input string: \"My. name. is Bond._James Bond!\" Output 2 strings:

  1. \"My. name. is Bond\"
  2. \"_James
4条回答
  •  难免孤独
    2020-12-28 12:10

    string[] theSplit = inputString.Split('_'); // split at underscore
    string firstPart = theSplit[0]; // get the first part
    string secondPart = "_" + theSplit[1]; // get the second part and concatenate the underscore to at the front
    

    EDIT: Following from the comments; this only works if you have one instance of the underscore character in your input string.

提交回复
热议问题