Spaces in string

I need to perform the following two actions ona string of variable length in c#

1. Insert a space every four charactors

2. Replace every charactor after the fourth charactor with a set charactor (e.g. #), excluding the newly inserted spaces

e.g.

oiukllksfjjkhadbfljhlfkj

1 = oiuk llks fjjk hadb fljh lfkj

2 = oiuk #### #### #### #### ####

Thanks
jonnyboy69Asked:
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.

NipNFriar_TuckCommented:
Hmmm... I think this can be done with a regex... otherwise you will probably end up looping... try:
using System.Text.RegularExpressions;
string myString = "oiukllksfjjkhadbfljhlfkj";

Regex regex1 = new Regex(
    @"(\w{4})",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

regex1.Replace(myString, @"${1} "); // The ${1} is to use the matched portion in the replace.  In a normal regex this works.. not sure if this is the right syntax here...

Regex regex2 = new Regex(
    @"\w",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

regex2.Replace(myString, @"#", 5); // strart replaceing after the 5 character...


hope this helps...

0

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
NipNFriar_TuckCommented:
Sorry, the code should actually be...

            string myString = "oiukllksfjjkhadbfljhlfkj";

            Regex regex1 = new Regex(
                @"(\w{4})",
                RegexOptions.IgnoreCase
                | RegexOptions.Multiline
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
                );

            myString = regex1.Replace(myString, @"${1} "); // The ${1} is to use the matched portion in the replace.  In a normal regex this works.. not sure if this is the right syntax here...

            Regex regex2 = new Regex(
                @"\w",
                RegexOptions.IgnoreCase
                | RegexOptions.Multiline
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
                );

            myString = regex2.Replace(myString, @"#", -1, 5); // strart replaceing after the 5 character...
0
TheAvengerCommented:
A little bit simpler, however probably slower and not so "nice" method:

string original = "oiukllksfjjkhadbfljhlfkj";

string spaced = original;
int curPos = 4;
while (curPos < spaced.Length)
{
      spaced = spaced.Insert (curPos, " ");
      curPos += 5;
}

string numbered = "";
curPos = 0;
while (curPos < spaced.Length)
      if (curPos < 5 || curPos % 5 == 4)
            numbered = numbered + spaced[curPos];
      else
            numbered += "#";

The result is in spaced and numbered
0
BigGreenClenaMachineCommented:
ok i look at the problem like this. if you don't need chars from char 5 on you can use this

string str = "asdfasdfasdfasdf";
string s1 = str.Substring(0,4);
string s2 = new string('#',str.Length -s1.Length);
str = s1+s2;
for(int i = 0;i<str.Length;i=i+5)
{
str = str.Substring(0,i) + " " + str.Substring(i,str.Length-i);
}
Console.WriteLine(str);

if you need the string that looks like "asdf asdf asdf asdf"

use this:

string str = "asdfasdfasdfasdf";
string str1 = str;
for(int i = 0;i<str1.Length;i=i+5)
{
str1 = str1.Substring(0,i) + " " + str1.Substring(i,str1.Length-i);
}
Console.WriteLine(str1); //output: "asdf asdf asdf asdf"

string s1 = str.Substring(0,4);
string s2 = new string('#',str.Length -s1.Length);
str = s1+s2;
for(int i = 0;i<str.Length;i=i+5)
{
str = str.Substring(0,i) + " " + str.Substring(i,str.Length-i);
}

Console.WriteLine(str); //output: "asdf #### #### ####"
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.