?
Solved

Spaces in string

Posted on 2004-11-03
4
Medium Priority
?
249 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:jonnyboy69
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 1000 total points
ID: 12484433
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
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12484473
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12484813
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
 
LVL 1

Expert Comment

by:BigGreenClenaMachine
ID: 12486700
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question