Avishekh Bharati
Avishekh Bharati

Reputation: 1918

How to split a text having a character which is combination of comma and space?

I have a following line which I want to split it by comma.

"Clark Kent,Hello Mr.Wayne,发送于 3:38 PM。"

Sounds easy right? The problem is the text does not contain single comma character. The commas you see in the text is a single character which is combination of comma and space(Just copy paste the above sentence in your text editor and check it out).

The problem is: I need to split the text by comma. Although I can copy paste the character add it as one of my delimiter characters, I am wondering if I could just convert such texts into text that could be splitted by comma. Well don't worry about Chinese words for now. The similar is the case with the last character you see in the text. Actually, this behavior arises when my application Language is set to Chinese.

enter image description here

FYI: I thought such comma is non printable/non ascii char but to my surprise, when I printed the text in console, I got:

enter image description here

Here is my input and expected output:

Input : "Clark Kent,Hello Mr.Wayne,发送于 3:38 PM。"

Expected output: {"Clark Kent", "Hello Mr.Wayne", "发送于 3:38 PM。"}.

Upvotes: 0

Views: 427

Answers (2)

Chris
Chris

Reputation: 597

The comma you are facing is a 'Fullwidth Comma' (Hex: 0xff0c). A regular unicode character which can be replaced by a comma and space using the string.Replace method:

s.Replace("<fullwidthComma>", "<trueComma><space>");

Upvotes: 1

Gaurang Dave
Gaurang Dave

Reputation: 4046

What I suggest (same as @Chris suggested in comments) is to replace your strange comma value with regular comma before split.

var s = "Clark Kent,Hello Mr.Wayne,发送于 3:38 PM。";
s = s.Replace(',', ',');
var splitted = s.Split(',');

Benefit is that if it find strange comma, it will replace else it will process with regular comma.

Upvotes: 0

Related Questions