String Manipulation/Adding a Character

I have a specific issue hopefully someone can help with. I currently have the string “Full Time : Regular” and I want to replace/enter a hyphen where the space between full and time is. I’m not sure of the best practice.

I don’t want to do a simple replace on the space because I want to keep the other spaces. I was thinking of trimming the first 4-5 characters (Full) and then concatenating a hyphen on the end? But I’m not sure how to go about this.

I would appreciate any help, thanks.

Just a quick answer, but you can use Regex.Replace()

System.Text.RegularExpressions.Regex.Replace(text, "(?<=(Full))(.*)(?=(Time))", "-")

The ?<= is a look behind and ?= is look ahead, so it replaces everything between those words, not including those words… or should, if I did it right :stuck_out_tongue:

Regards.

1 Like

@ClaytonM always has the best answers but you can replace more than just single characters with string.replace. For simplicity, if it’s always “Full Time”, you could just do a string.replace(“Full Time”, “Full-Time”). If you have “Full Time” and “Part Time” in string variables, just string.replace(" Time", “-Time”)

1 Like

Good point @tmays. You can also just do text.Replace(“Full “,“Full-”) or text.Replace(” Time”,“-Time”)

That solution works but it adds an extra space that is not wanted. It looks like “Full -Time : Regular” but in order to be entered into the software the space after “Full” must not be there

Did you try both solutions?, and if there’s a dynamic chance that there are multiple spaces, then Regex might work better. Also check for typos if you included an extra space somewhere.

Regards.

I only tried the replacing Time with -Time solution because I had found another one on my own. I ended up converting to an array of characters, removing the space character, and inserting a hyphen in its spot.

Oh ok, well if you replace Time you need to replace " Time" instead of “Time”. Adding the space infront will replace the space also.

1 Like