473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

insert a string into another string at a certain position?

CK
Hi All,
Theis should be easy. I need to insert a string into an exisitng string?
Example
var string1 = "sweet";
var string2 = "bbb";

I want to modify string1 to be "swebbbet";
I know this must be farely simple but a little more than simple
concatenation is required.
Thanks in advance for any solutions.

Regards,
CK
Apr 25 '06 #1
2 20377
"CK" <c_**********@hotmail.com> writes:
Theis should be easy. I need to insert a string into an exisitng string?
Example
var string1 = "sweet";
var string2 = "bbb";

I want to modify string1 to be "swebbbet";


string1 = "swebbbet";

But seriously, I assume you have some position where you want it.
Then you can build the result by concatenating substrings:

string1 = string1.substring(0,3) + string2 + string1.substring(3);

You can't modify the string in the variable "string1" itself,
you can only create a new string and assign that to the variable.
Strings are immutable.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 25 '06 #2
JRS: In article <y7**********@hotpop.com>, dated Tue, 25 Apr 2006
23:11:15 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com> posted :
"CK" <c_**********@hotmail.com> writes:
Theis should be easy. I need to insert a string into an exisitng string?
Example
var string1 = "sweet";
var string2 = "bbb";

I want to modify string1 to be "swebbbet";
string1 = "swebbbet";

But seriously, I assume you have some position where you want it.


There must be some definition (even if it is "at random"), but the
condition may be given other than as a number.
Then you can build the result by concatenating substrings:

string1 = string1.substring(0,3) + string2 + string1.substring(3);


Or by

string1 = string1.replace(/(.{3})/, "$1" + string2) // position 3
string1 = string1.replace(/(e)/, "$1" + string2) // after first e
string1 = string1.replace(/(e[^e]+)/, string2 + "$1") // before ...
// ... the first e followed by a non-e
string1 = string1.replace(/(..)$/, string2 + "$1") // two from end
string1 = string1.replace(/ee/g, "e" + string2 + "e") // in every 'ee'

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 26 '06 #3

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

Similar topics

3
by: Ken Robinson | last post by:
This one has me stumped... Let's say I have a string consisting of n words (where n > 0). I would like to insert string 'abcd' between the first and second characters of each word of the string....
2
by: Paweł | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and...
2
by: Looks Like Trent | last post by:
Find a character in a string, at a specified position. How do you do this? Any help will be appreciated....
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
6
by: KJ | last post by:
How can you look for a character in a certain position with VB.Net and if the string length is less than the position have it return and empty string instead of an error? Substring returnns an...
3
by: magix | last post by:
Dear Guru, I have been thinking hard on how to token based on demiliter after certain position. Example, I have list of possible string below, and the the delimiter is "1" with the rules below...
3
by: magix | last post by:
How can I search for occurance of a character in certain position of a string I checked function strchr, but doesn't option to specify position. Thanks. Regards, Magix
3
by: AnagJohari | last post by:
I want to insert the record at any position in a grid view means means if i add Insert buuton at every row of grid view. & when i click on insert button of an perticular row a new row is created...
0
by: midhun singh | last post by:
Hi all, I have a little bit of a problem figuring out how to insert a textbox string from a VB.Net Windows form onto a Crystal Report. I have search through many articles and manuals for a way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.