简体   繁体   English

如何用逗号替换第一次出现的空格?

[英]How to replace first occurrence of whitespace with comma?

I have string as : 我的字符串为:

string sku= "plmca60     5";

I just wanted to replace first blank space with comma. 我只想用逗号替换第一个空格。 Hence I tried: 因此,我尝试:

sku.replace(" ",",");

But it given me: 但这给了我:

sku="plmca60,,,,,5";

Its because five blank spaces are there in the string. 这是因为字符串中有五个空格。 But I want to replace just first occurrence of blank space like; 但是我只想替换第一次出现的空白;

sku="plmca60,    5"

How can I do like this? 我该怎么做?

First of all, String.Replace method 1 returns your string as plmca60,,,,,5 because from documentation; 首先, String.Replace方法 1返回字符串为plmca60,,,,,5因为从文档开始;

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string. 返回一个新字符串,在该字符串中,当前实例中所有出现的指定字符串都被另一个指定字符串替换。

You don't need regex for that. 您不需要正则表达式。

You can use String.IndexOf(string) to get first index of a white space and a little bit Remove and Insert methods combination. 您可以使用String.IndexOf(string)来获取空白的第一个索引以及一点RemoveInsert方法的组合。

Reports the zero-based index of the first occurrence of the specified string in this instance 报告在此实例中第一次出现的指定字符串的从零开始的索引

string sku = "plmca60     5";
int index = sku.IndexOf(" ");
sku = sku.Remove(index, 1).Insert(index, ",");
Console.WriteLine(sku);

Result will be; 结果将是;

plmca60,    5

Here a demonstration . 这里有demonstration

1: Remember, it is Replace , not replace . 1: 请记住,它是Replace而不是replace C# is case sensitive language. C#是区分大小写的语言。

You could use capturing groups like below, 您可以使用以下捕获组,

@"^(\S*)\s"

OR 要么

@"^(\S*) "

Replace the matched space by, 将匹配的空间替换为

$1,

DEMO 演示

\\S matches a non-whitespace character. \\S匹配非空格字符。 \\S* matches zero or more non-whitespace characters. \\S*匹配零个或多个非空白字符。 ^ asserts that we are at the start. ^断言我们是开始。

Code: 码:

string sku= "plmca60 5";
string result = Regex.Replace(sku, @"^(\S*) ", "$1,");
Console.WriteLine(result);
Console.ReadLine();

IDEONE 爱迪生

By using Regex.Replace 通过使用Regex.Replace

var regex = new Regex(Regex.Escape(" "));
var newText = regex.Replace("plmca60     5", ",", 1);

Not an optimized solution but it works, 不是优化的解决方案,但它可以工作

string sku = "plmca60     5";
string[] skyArr = sku.Split(' ');
skyArr[1] = ",";
string resultString = "";
foreach (string str in skyArr) 
{
     if (str == "")
         resultString += " ";
     else
         resultString += str;
     }
}
Console.WriteLine(resultString);

You can replace all whitespace with ", ". 您可以将所有空格替换为“,”。 For example: 例如:

sku = Regex.Replace(sku, @"\\s+", ", ");

            string sku = "plmca60     5";
            int pos = sku.IndexOf(" ");
            char[] a = sku.ToCharArray();
            a[pos]=a[pos].ToString().Replace(" ", ",").ToCharArray()[0];
            var result = new string(a);

This is dirty way. 这是肮脏的方式。 Meanwhile trying to find out shorter way of doing this. 同时尝试找出更短的方法。

I would capture 1 space with 0 or more spaces after that in a capture group. 在捕获组中,我将捕获0个或多个空格之后的1个空间。 That way you can replace it with a comma and the content of the capture-group (which was any spaces after the first one) 这样,您可以将其替换为逗号和捕获组的内容(第一个之后的空格)

Regex.Replace(input, @"\s(\s*)", ",$1");

Online demo 在线演示

Two line version: 两行版本:

string sku = "plmca60     5";
Console.WriteLine(sku.Insert(sku.IndexOf(" ")).Remove(sku.Index(" "));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM