PDA

View Full Version : Add a space to a string after each comma.


samira02
08-12-2002, 03:18 PM
I have a string that will be dynamic but will look like this: '111111111,221212121,123214541,455554845' (etc)
What I need to do is add a space after each comma, the length of the numbers after each comma will always be 9. So I need the above string to look like this.
'111111111, 221212121, 123214541, 455554845'

Thanks in advance for your help,

samira02
08-12-2002, 03:31 PM
Actually, the string comes in as such" '012122221 122222215 455445544' What I need is to add a comma and leave one space after the comma. So the result should be.
'012122221, 122222215, 455445544'

Right now, with the code I have the result is '012122221,122222215,455445544' Notice the space after comma is gone.
Here is the code I am using




<script language="javascript">

var strNIINList;

function LTrim(str)
/***
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim

RETVAL: An LTrimmed string!
***/
{
var whitespace = new String(" ,;|\t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...

var j=0, i = s.length;

// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;


// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}

return s;
}

function RTrim(str)
/***
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim

RETVAL: An RTrimmed string!
***/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" ,;|\t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...

var i = s.length - 1; // Get length of string

// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;


// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}

return s;
}


function trim(str)
/***
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim

RETVAL: A Trimmed string!
***/
{
return RTrim(LTrim(str));
}

function IsDelimiter(TheString){
//alert(escape(TheString));
//if (TheString == "," || TheString == ";" || TheString == " " || TheString == "|"){
if (escape(TheString) == "%2C" || escape(TheString) == "%3B" || TheString == " " || escape(TheString) == "%0D" || escape(TheString) == "%0A" || escape(TheString) == "%7C") {
return (true);
}
else{
return (false);
}
}


function ValidateNIIN(TheNIIN){
if(TheNIIN.length != 9 && TheNIIN.length != 0){
alert("NIIN " + TheNIIN + " is " + TheNIIN.length + " characters long. It needs to be 9 characters to be valid");
}
else{
strNIINList = strNIINList + TheNIIN + ",";
}
}

function Check(TheForm){
//todo:
// check each NIIN for length = 9
// valid delimiters are space, comma, semicolon,pipeline
// could be NIINs entered with no delimeter
//
//loop through the string until we come across a valid delimiter
var strString;
var intStartPos;
var intLastDelimiterPos;

strNIINList = ""
strString = trim(TheForm.MultNiin1.value)
//alert("String Length: " + strString.length);
alert(escape(strString));
intStartPos = 0;

//alert(strString.substring(6,8));

for(x = 0;x <= strString.length - 1;x++){
//alert(x);
if(x == (strString.length - 1)){
ValidateNIIN(strString.substring(intStartPos,x+1));
}

if(IsDelimiter(strString.substring(x,x+1))){
//alert("Current Position: " + x);
//alert("Delimiter found at position: " + x);
//alert("StartPos: " + intStartPos);
//alert("EndPos: " + x);
ValidateNIIN(strString.substring(intStartPos,x));
intLastDelimiterPos = x;
intStartPos = x + 1;
}
}

//alert(strNIINList);
var arrayofstrings;
var newarray;

newarray = new Array();

arrayofstrings = strNIINList.split(",");
//alert(arrayofstrings.length)
//alert(arrayofstrings[1]);
y = 0;
for(x=0;x< arrayofstrings.length;x++){
//alert("here");
//alert((arrayofstrings[x]));
if(arrayofstrings[x] != ""){
//alert("here2");
newarray[y] = arrayofstrings[x];
y++;
}
}

//alert(newarray);
TheForm.txtniinhidden1.value = newarray
return (true);
}

</script>




<form method="POST" action="Test.asp" language="JavaScript" name="FrontPage_Form4" onsubmit="return Check(this)">

<% End If %>
<input type="hidden" name="System_Level" value="INDIVIDUAL"><div align="left"><table border="0" width="100%">
<tr>
<td></td>
</tr>
<TR>

<td align=middle>&nbsp; <TEXTAREA id=MultNiin1 name=MultNiin1 rows=20></TEXTAREA>
<input type="hidden" id="txtniinhidden1" name="txtniinhidden1">
</td></TR>
<TR>
<td align="middle" class="redfonttwelve">
<P><INPUT id=submit1 type=submit value=Submit name=submit1></P>

</P>
</td></TR></table></div></form></td>
</tr>
</table>
</form>

nolachrymose
08-12-2002, 03:50 PM
Try this:

var newStr=dynamicStr.split(" ").join(", ");

Hope that helps!

Happy coding! :)

samira02
08-12-2002, 04:35 PM
When I add this code, all I get back is a string of commas ,,,,,,,,,,,,,,, as such. Maybe I am not adding the code in the right spot. This is what I did :

changed this line:
arrayofstrings = strNIINList.split(",");

To:

arrayofstrings = strNIINList.split(",").join(" ");

Thanks

BrightNail
08-12-2002, 11:52 PM
okay, your code seems very heavy for what you want to do...but lets just 'fix' what you currently have...

you say that your current code generates the appropriate result sans the space after the comma...

okay....just add this.....

var addBlank =/\,/g;
var totalVal = newarray;
var joinIt = totalVal.replace(addBlank,", ");

TheForm.txtniinhidden1.value=joinIt;

you see what I did?...add the first two variables at the top of the javascript...just to declare them...
add the 'joinit' variable right abouve where you 'return' the value....

then add the "form element"..like you already have...but instead of giving it the value...newarray...we give it "joinit"...

what we did was suck up the value of "newarray"...and look thru it...whenever there is a comma (variable addBlank...is global)...we replace the comma with a comma AND a space (joinit).

hope it helps.

james

adios
08-13-2002, 12:10 AM
Thank God for regular expressions:

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

String.prototype.replaceSpaceWithComma = function() {
return this.replace(/\b\s\b/g, ', ');
}

var x = '012122221 122222215 455445544';
document.write(x);
document.write('<br>');
document.write(x.replaceSpaceWithComma());

</script>
</head>
<body>
</body>
</html>

All your code could be greatly simplified with regexes...

samira02
08-13-2002, 04:16 PM
Thanks for your help, but it's still not working.

BrightNail, I tried what you told me, but I don't get any results back. I don't get an error either, but the return value is blank.
Any ideas? Thanks so much.

BrightNail
08-13-2002, 09:06 PM
samurai,,,

hey...its probably not working becaus I don't know for sure...what variable holds your result.....at the end..

var totalVal = newarray;

that newarray variable is what I assumed was yours...

so....what variable IN YOUR CODE holds your numbers..."1234,23345,5677,78990 etc..."

that VARIABLE...apply to this.......

var totalVal= WHAT???? put your variable here..

send me your html page..and I'll fi it..

samira02
08-14-2002, 03:59 PM
hi,
here is my page, with your code added. What am i doing wrong?
newarray variable is what holds the numbers.
Thanks much,
samira


<script language="javascript">

var strNIINList;

function LTrim(str)
/***
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim

RETVAL: An LTrimmed string!
***/
{
var whitespace = new String(" ,;|\t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...

var j=0, i = s.length;

// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;


// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}

return s;
}

function RTrim(str)
/***
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim

RETVAL: An RTrimmed string!
***/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" ,;|\t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...

var i = s.length - 1; // Get length of string

// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;


// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}

return s;
}


function trim(str)
/***
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim

RETVAL: A Trimmed string!
***/
{
return RTrim(LTrim(str));
}

function IsDelimiter(TheString){
//alert(escape(TheString));
//if (TheString == "," || TheString == ";" || TheString == " " || TheString == "|"){
if (escape(TheString) == "%2C" || escape(TheString) == "%3B" || TheString == " " || escape(TheString) == "%0D" || escape(TheString) == "%0A" || escape(TheString) == "%7C") {
return (true);
}
else{
return (false);
}
}


function ValidateNIIN(TheNIIN){
if(TheNIIN.length != 9 && TheNIIN.length != 0){
alert("NIIN " + TheNIIN + " is " + TheNIIN.length + " characters long. It needs to be 9 characters to be valid");
}
else{
strNIINList = strNIINList + TheNIIN + ",";
}
}

function Check(TheForm){
//todo:
// check each NIIN for length = 9
// valid delimiters are space, comma, semicolon,pipeline
// could be NIINs entered with no delimeter
//
//loop through the string until we come across a valid delimiter
var strString;
var intStartPos;
var intLastDelimiterPos;

strNIINList = ""
strString = trim(TheForm.MultNiin1.value)
//alert("String Length: " + strString.length);
alert(escape(strString));
intStartPos = 0;

//alert(strString.substring(6,8));

for(x = 0;x <= strString.length - 1;x++){
//alert(x);
if(x == (strString.length - 1)){
ValidateNIIN(strString.substring(intStartPos,x+1));
}

if(IsDelimiter(strString.substring(x,x+1))){
//alert("Current Position: " + x);
//alert("Delimiter found at position: " + x);
//alert("StartPos: " + intStartPos);
//alert("EndPos: " + x);
ValidateNIIN(strString.substring(intStartPos,x));
intLastDelimiterPos = x;
intStartPos = x + 1;
}
}

//alert(strNIINList);
var arrayofstrings;
var newarray;

var addBlank =/\,/g;
var totalVal = newarray;



newarray = new Array();

arrayofstrings = strNIINList.split(",");
//alert(arrayofstrings.length)
//alert(arrayofstrings[1]);
y = 0;
for(x=0;x< arrayofstrings.length;x++){
//alert("here");
//alert((arrayofstrings[x]));
if(arrayofstrings[x] != ""){
//alert("here2");
newarray[y] = arrayofstrings[x];
y++;
}
}

var joinIt = totalVal.replace(addBlank,", ");
//alert(newarray);
TheForm.txtniinhidden1.value = joinIt
return (true);
}

</script>




<form method="POST" action="Test.asp" language="JavaScript" name="FrontPage_Form4" onsubmit="return Check(this)">

<% End If %>
<input type="hidden" name="System_Level" value="INDIVIDUAL"><div align="left"><table border="0" width="100%">
<tr>
<td></td>
</tr>
<TR>

<td align=middle> <TEXTAREA id=MultNiin1 name=MultNiin1 rows=20></TEXTAREA>
<input type="hidden" id="txtniinhidden1" name="txtniinhidden1">
</td></TR>
<TR>
<td align="middle" class="redfonttwelve">
<P><INPUT id=submit1 type=submit value=Submit name=submit1></P>

</P>
</td></TR></table></div></form></td>
</tr>
</table>
</form>