c# replace every 3 spaces by newline in a string

Hi,

I'd like a quick logic to replace every 3 spaces found in the string by new line.
Can You help?
Thanks,
JT
jtran007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
please one of following
s = s.Replace("   ", Environment.NewLine);
s = s.Replace("   ", "\n");
Thanks
khan_webguruCommented:
Hi,

string trim = Regex.Replace( text, "   ", @"\n" );

text would be your string.
khan_webguruCommented:
You could also use regular expressions, like this

string in_string = "Hello   World   Hi";

Regex reg = new RegEx("   ");

string out_string = reg.Replace(in_string, @"\n");
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

jtran007Author Commented:
Hi,
Thanks but I want to replace every 3 spaces with a new line, not every space with new line.

Thanks,
JT
khan_webguruCommented:
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{3,}", options);    
tempo = regex.Replace(tempo, @"\n");

that would replace your 3 consecutive space with \n

reference URL:  http://stackoverflow.com/questions/206717/how-do-i-replace-multiple-spaces-with-a-single-space-in-c

tempo is your string object
jtran007Author Commented:
Hi khan,

It's not working.
Thanks,
JT
khan_webguruCommented:
Hi,

How can be its possible. I implemented here on my end for you and verified then sent to you.

 
string tempo= "12345 6  7   8 8    9";
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[ ]{3,}", options);
            tempo = regex.Replace(tempo, @"\n");

            ErrorMsg.Text = tempo;

Open in new window


Thats code I added in button click event and then in code behind I just add above mentioned code and got the attached image result.

 Result
See the number where I put 3 or more spaces 3 consecutive that are replace with "\n"

Forget about all other controls I just added the code on "Process Sample File" click event in code behind when you will use this code just need to right click and in "Resolve" menu you will find "System.Text.RegularExpressions" just click to add the reference to avoid the error means just import the reference on above of the page like this

 
using System.Text.RegularExpressions;

Open in new window

khan_webguruCommented:
Very Strange :) I cross verified again and got running again hope that will help you as well to solve this issue.

Thanks & Regards,

AAK
jtran007Author Commented:
Hi,

Sorry, if i replace ',' instead of  space. what I should change?

Thanks,'
JT
khan_webguruCommented:
First I tested this locally now posting the code so that it will not give you any problem,

 
string tempo= "12345,6,,7,,,8 8,,,. 9,,,";
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[,]{3,}", options);
            tempo = regex.Replace(tempo, @"\n");

            ErrorMsg.Text = tempo;

Open in new window


Regular Expressions are those who needs very much concentrations :)
jtran007Author Commented:
Hi,

Here mycode:
using System.Text.RegularExpressions;
...

            string s = "lable part 01,lable part 02,lable part 03,lable part 04,lable part 05,lable part 06,lable part 07,lable part 08,lable part 09";
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[,]{3,}", options);
            s = regex.Replace(s, @"\n");
            Console.WriteLine(s);

it is not working. I don't know why?

Thanks,
JT
khan_webguruCommented:
in this code there is no consecutive three , means like this ,,, if you want to replace single coma then use like this

 
string s = "lable part 01,lable part 02,lable part 03,lable part 04,lable part 05,lable part 06,lable part 07,lable part 08,lable part 09";
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[,]{1,}", options);
            s = regex.Replace(s, @"\n");

            Console.WriteLine(s);

Open in new window

khan_webguruCommented:
Sorry I thought you want to replace consecutive three commas ",,,". But now this above code will replace this one comma by \n
jtran007Author Commented:
Hi,

Yes, I mean three commas (not consecutive) in a series of string that ends with comma.

Thanks,
JT
khan_webguruCommented:
See my post number #a35286147

Thats is the code that will replace series of the commas in a string

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khan_webguruCommented:
OR

if you want to replace only first 3 commas in a string with "\n" then I produce a small peace of code just add the below mention method in your code

 
private string replaceFirstThreeCommas(string input, int numberOfReplaces)
        {
            int count = 0;
            string resultString="";
            for (int i = 0; i < input.Length; ++i)
            {
                if (input[i].ToString() == "," && count < numberOfReplaces)
                {
                    resultString += @"\n";
                    count++;
                }
                else
                {
                    resultString += input[i].ToString();
                }
            }
            return resultString;
        }

Open in new window


Now call the method like below

 
string s= "12345,6,,7,,,8 8,,,. 9,,,";          
s = replaceFirstThreeCommas(s, 3);
Console.WriteLine(s);

Open in new window


First parameter of the method is your string on which you want to perform action and "3" means that will replace first 3 comma found replace with "\n" if you want to change the number like 4 so it will replace first 4 commas.

hope that will help

Regards,

Asif Ahmed Khan
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
use following code, " "  contains 3 spaces it seems you are considering it as one


s = s.Replace("   ", Environment.NewLine);
Or
s = s.Replace("   ", "\n");


s = s.Replace("s1s2s3", Environment.NewLine);
Or
s = s.Replace("s1s2s3", "\n");
where s1s2s3 are three spaces

Open in new window

kaufmed 👽Commented:
Can you clarify the requirement? You say "every 3 spaces," which everyone is taking to mean 3 spaces should be turned into 1 newline. Do you mean instead, "every 3rd space", such that:

    [space][space][space]

would become

    [space][space][newline]

???
kaufmed 👽Commented:
In the event you indeed mean the latter, try either of these:
string source = "Hello   World!";
// =======================================================================

source = source.Replace("   ", "  \n");

--OR--

source = System.Text.RegularExpressions.Regex.Replace(source, "(?<=  ) ", "\n");

Open in new window

jtran007Author Commented:
Thanks,
JT
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.