-4

I have two strings:

 string ThePhone = "XXX-XXX-XXXX"
 string SkypeBlock = "-<span style='display:none;'>-</span>";

I want to replace the last dash (-) in ThePhone with SkypeBlock

And the desired result:

TheNewPhone = "XXX-XXX-<span style='display:none;'>-</span>XXXX"

How?

frenchie
  • 51,731
  • 109
  • 304
  • 510

2 Answers2

2

Here is your solution

var ThePhone = "XXX-XXX-XXXX";
var SkypeBlock = "<span style='display:none;'>-</span>";
var lastDash = ThePhone.LastIndexOf('-'); // Find position of the last dash
var theNewPhone = thePhone.Remove(lastDash, 1).Insert(lastDash, SkypeBlock); // Replace last dash
Peter PAD
  • 2,252
  • 1
  • 17
  • 20
1

I guess this should do the trick:

string ThePhone = "XXX-XXX-XXXX";
string SkypeBlock = "-<span style=\"display:none;\">-</span>";

ThePhone = ThePhone.Substring(0, 8) + SkypeBlock + ThePhone.Substring(8, 4);
frenchie
  • 51,731
  • 109
  • 304
  • 510
RajeevNy
  • 28
  • 6