Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inserting a space into String

Posted on 2010-08-16
7
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33449747
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
ID: 33449763

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

Open in new window

0
 
LVL 30

Expert Comment

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

Just fix the line:

a = a.Insert(4, " ");
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 664 total points
ID: 33449793
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 668 total points
ID: 33449837
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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 668 total points
ID: 33449958
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
ID: 33508838
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

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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