...

View Full Version : String Question



jason_kelly
12-23-2011, 04:56 PM
Hello,

I need your help.

Let's say for example if I have the following file number: 2011-1234567 and if the value of the variable is already the same (ie. var fileNo = 2011-1234567) how can I go about automatically adding a number at the end?

ex.1.)
var fileNo = "2011-1234567"
if (fileNo already exists) { fileNo = 2011-1234567-2 }

ex.2.)
var fileNo = "2011-1234567-2"
if (fileNo already exists) { fileNo = 2011-1234567-3 }

ect.

Much thanks and appreciation for all your help.

Jay

devnull69
12-23-2011, 05:00 PM
You'll need the concatenate operator for strings (which is "+")

Example


var myString = "2011-1234567";
myString = myString + "-2";
// myString will now be 2011-1234567-2

jason_kelly
12-23-2011, 05:08 PM
Thanks very much for this Dev Null,

How can I have it auto increment the number everytime the same fileno exists:


var myString = "2011-1234567";
myString = myString + "-2";

if (2011-1234567-2 already exists) inc end# and output new file number as:

2011-1234567-3

Thanks a bunch

Jay

xelawho
12-23-2011, 05:09 PM
you can try this:



<body>

input number: <input type="text" id="box"> <input type ="button" value="check number" onclick="checkNum()">
<div id="results"></div>
<script type="text/javascript">
nums=[100, 200, 300]
function checkNum() {
var inp= document.getElementById("box").value;
for(var j = 0; j<nums.length; j++){
if (nums[j]==inp) {
inp=inp+"-2"
}
}
document.getElementById("results").innerHTML="Unique reference is "+inp;
nums.push(inp);
}
</script>

</body>


but this is a simple example and you will want to test it - say you input 100, that will be changed to 100-2. But if you then input 100-2 that will be changed to 100-2-2 and so on... it can be developed further (changing the second input to 100-3), if that's what's required...

xelawho
12-23-2011, 05:56 PM
oops - I think we posted at the same time. There's probably a fancier way to do this, but I am not very good at regex. This seems to work, provided that your original numbers will always have the dash in that location like this: 2011-1234567



<body>

input number: <input type="text" id="box"> <input type ="button" value="check number" onclick="checkNum()">
<div id="results"></div>
<script type="text/javascript">
var inp;
nums=[100, 200, 300]
function checkNum() {
inp= document.getElementById("box").value;
for(var j = 0; j<nums.length; j++){
if (nums[j]==inp) {
if (inp.lastIndexOf("-")==4){
inp=inp+"-2"
}else {
newnum=Number(inp.match(/[^\-]*$/))+1
inp=inp.replace(/[^\-]*$/,newnum)
}
}
}

document.getElementById("results").innerHTML="Unique reference is "+inp;
nums.push(inp);
}
</script>

</body>

alykins
12-23-2011, 06:49 PM
i would think you would want to go about it by passing parameters (I can't give exact js code bc i am not well versed but the logic behind it i would think could be)


function(obj){
foreach string filename in C:\\ *i have no clue where r r comparing if string.exists
{
if (filename.length == obj.length)
string[] test = null;
string[] compare=null;
test[] = filename.split('-');
compare[]=obj.split('-');
if (test.length == compare.length)
if(test.length == 1)
{test[0] = test[0] + '-1';}
else if () // need to compare the arrays... can you just do a "does array1 == array2"
// in js? i dont think so in which case just make a for loop comparing each
// spot in the array...
else if (test[test.length-1] == compare[compare.length-1])
{
int i = Convert.ToInt(test[test.length-1]);
i++;
var _i = i.tostring();
_i = '-' + i;
test[test.length-1] = _i;
var SaveFileName = test.tostring();
}
}


need to convert that to javascript it is a hybrid monster right now... but essentially you pass the string number and split it on '-' and then whatever/where-ever your comparing against do the same... if they have no '-' then their lengths will be equal and if their langth is == 1 then they are a one size number otherwise you drop down and look at the last spot on the array and see if it is equal, if it is add one and convert back to string concat the '-' and convert the array back to string format



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum