String Manipulation/Adding a Character

string

#1

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.


#2

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.


#3

@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”)


#4

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


#5

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


#6

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.


#7

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.


#8

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