Prithvi Raj Nandiwal
Prithvi Raj Nandiwal

Reputation: 3294

Add space between Number and String

I have a string Like this

"My Train Coming on Track 10B on 6A and string test with 11S"

Now i want to Add space between Number like 11 and Char B and so on i want to like this

"My Train Coming on Track 10 B on 6 A and string test with 11 S"

using C#. is there any logic for that. thank

Upvotes: 1

Views: 3906

Answers (2)

Amit Sengar
Amit Sengar

Reputation: 131

The Above answer is correct But according to requirement we have to use that like :

  var result = Regex.Replace("4A", @"(?=\p{L})(?<=\d)", " ");

I hope it will help you.

Upvotes: 3

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

With a regex:

var result = Regex.Replace(str, @"(?<=\d)(?=\p{L})", " ");

This replaces the "empty space" between a digit ((?<=\d)) and a letter ((?=\p{L})) with a space.

A different method without the lookarounds would be:

var result = Regex.Replace(str, @"(\d)(\p{L})", "$1 $2");

In this case, it replaces the last digit and the first letter with the pattern $1 $2, inserting a space in the process.

Upvotes: 10

Related Questions