473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I make a string longer??

hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
Jan 25 '07 #1
6 1417
JR
Pad

JR
"roger_27" <ro*****@discus sions.microsoft .comwrote in message
news:A5******** *************** ***********@mic rosoft.com...
hey, I have a bunch of strings that are 20 characters long. The user
inputs
data 10 characters long and I am inserting data at character spot number
11.

well what if the user inputs data that is 5 characters long? how do I
insert
spaces untill it is 10 characters long, so I can still use the
string.insert
at space number 11?

I hope this made sense. thanks.

Jan 25 '07 #2
Hi,

Use PadLeft or PadRight. Or better still...what about StringBuilder?

Brian

On Jan 25, 12:27 pm, roger_27 <roge...@discus sions.microsoft .com>
wrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
Jan 25 '07 #3
Hi,
"roger_27" <ro*****@discus sions.microsoft .comwrote in message
news:A5******** *************** ***********@mic rosoft.com...
| hey, I have a bunch of strings that are 20 characters long. The user
inputs
| data 10 characters long and I am inserting data at character spot number
11.
|
| well what if the user inputs data that is 5 characters long? how do I
insert
| spaces untill it is 10 characters long, so I can still use the
string.insert
| at space number 11?

Remember that String is an inmutable class, this means that if you try to
modify it in any way you are creating a new instance.

With that remark in place you can use Pad or simply adding a new string like

corrected = user_input + new String( ' ', 10 - user_input);
Take also a look at StringBuilder class

--
Ignacio Machin
machin AT laceupsolutions com
Jan 25 '07 #4
roger_27 <ro*****@discus sions.microsoft .comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.
string padded = originalString. PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #5
because Im replacing this same string many times... there is quite a crazy
situation... basically i take a huge string, and pass pieces of the string 17
characters in length to a printer. also, add text to the end of it that says
"Line 1" "Line 2" "Line 3"

but what if the last line is less than 17 characters? I cant add the line
number just to the end,. it wont be right justified with the rest of the line
numbers. so I need to add spaces till its long enough,.

its actually more complicated than that. but I'm just showing you a perfect
example of where I need to do this.

"Jon Skeet [C# MVP]" wrote:
roger_27 <ro*****@discus sions.microsoft .comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.

string padded = originalString. PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 25 '07 #6
On Thu, 25 Jan 2007 14:35:01 -0800, roger_27 <ro*****@discus sions.microsoft .com>
wrote:
>because Im replacing this same string many times... there is quite a crazy
situation... basically i take a huge string, and pass pieces of the string 17
characters in length to a printer. also, add text to the end of it that says
"Line 1" "Line 2" "Line 3"

but what if the last line is less than 17 characters? I cant add the line
number just to the end,. it wont be right justified with the rest of the line
numbers. so I need to add spaces till its long enough,.

its actually more complicated than that. but I'm just showing you a perfect
example of where I need to do this.

"Jon Skeet [C# MVP]" wrote:
>roger_27 <ro*****@discus sions.microsoft .comwrote:
hey, I have a bunch of strings that are 20 characters long. The user inputs
data 10 characters long and I am inserting data at character spot number 11.

well what if the user inputs data that is 5 characters long? how do I insert
spaces untill it is 10 characters long, so I can still use the string.insert
at space number 11?

I hope this made sense. thanks.

string padded = originalString. PadRight(10);

(If you've always got an original string which is 10 characters long
though, why are you using Insert to append to the end of it? Why not
just use string extra = original + otherBit; ?)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
string test = "test";

string output = string.Format(" {0,-17}{1}", test, "Line 1");

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8003
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
5
7309
by: Gandalf | last post by:
Hello. I thought that strlen was only available after adding #include <string> to my c++ program, but apperently not, or the compiler is clever enough to find it by it self, or what? The program compiles and runs fine on my Linx box, with g++ version 2.95.4
5
1774
by: Doug Goulden | last post by:
I have an application library that I have been using for some time with no problems. Yesterday though I started to use part of the library and found that the results were much different than I expected. My problem is in using the std::string class, when I create a string by calling its constructor or trying to set its value, the value of the string object is incorrect. The difference between my previous use and what I am doing no is that...
51
8288
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
6
1390
by: Mark Rae | last post by:
Hi, Is there any in-built converter which will convert a string like markrae into markrae
17
8146
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found characters") Else Debug.WriteLine("Did NOT find characters!") End If
53
4092
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
15
1816
by: Lighter | last post by:
I find a BIG bug of VS 2005 about string class! #include <iostream> #include <string> using namespace std; string GetStr() { return string("Hello");
34
2663
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for string concatenation: x = "a" + "b"
5
1302
by: Gary Townsend | last post by:
I have been working on a way to take a datatable from a dataset and transform it into a Geography Markup Language(GML) stream so it can be loaded into another component. The problem i'm finding is that the building of the string to transform it into a stream is taking longer and longer as you add more data to the string ( as you can see from the printout below. Any suggestions for reducing the time? Should i be writing the GML to the...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.