Solved

Inserting a space into String

Posted on 2010-08-16
7
400 Views
Last Modified: 2012-05-10
HI All,

Lets say I have the following random string - ox228rg,

Could anyone show me a C# example of inserting a space before the 3rd character on the right so that the string looks like so - ox22 8rg

Many thanks,

Rit
0
Comment
Question by:rito1
7 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
Try:
string s = "ox228rg";

            string s2 = s.Substring(0, s.Length - 3) + " " + s.Substring(s.Length - 3, 3);



            MessageBox.Show(s2);

Open in new window

0
 
LVL 10

Expert Comment

by:Jini Jose
Comment Utility

string a = "ox228rg";

            char sp = Convert.ToChar(" ");

            a.Insert(4, " ");

            Console.Write(a);

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
gmailjini, you have made a common mistake. The output will be : "ox228rg"!

Just fix the line:

a = a.Insert(4, " ");
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 166 total points
Comment Utility
string s = "ox228rg";
s = s.Length <= 3 ? s : s.Insert(s.Length-3, " "); // handle short strings
0
 
LVL 10

Accepted Solution

by:
Jini Jose earned 167 total points
Comment Utility
sorry.
you can use the below one

string a = "ox228rg";
            char sp = Convert.ToChar(" ");
            string b=a.Insert(4, sp.ToString());

            Console.Write(b);
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 167 total points
Comment Utility
If go with  code, I'd still modify it to insert not in to the 4th index, but before the 3d from the end character:

Great respect to NZ kiwis! :) Very good idea to check for short strings!
            string a = "ox228rg";

            char sp = Convert.ToChar(" ");

            a = a.Insert(a.Length-3, " ");



            MessageBox.Show(a);

Open in new window

0
 
LVL 1

Author Comment

by:rito1
Comment Utility
Hi All,

Thanks for you input. Apologies for the delay.. sickness struck!... I am picking this back up today so will respond shortly.

Regard,

Rit
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now