Jump to content
atomic

Javascript - Substring Ada Masalah

Recommended Posts


var strConvert = "A%2F2%2E2%28C%29%27";
var arySplit = strConvert.split("%");

if (Object.prototype.toString.call(arySplit) === '[object Array]') {
    for (var i = 0; i < arySplit.length; i++) {
        if (i === 0) {
            strOutput = arySplit[i];
        }
        if (i !== arySplit.length - 1) {
            strHex = "%" + arySplit[i + 1].substring(0, 2);
            Letter = decodeURIComponent(strHex);
            strOutput = strOutput + Letter + arySplit[i + 1].substring(arySplit[i + 1].length-1);
        }
    }
alert(strOutput);
}

salam otai semua, berdasarkan script kat atas, output skrg adalah  :

 A/2.2( C )9'7

output yg sepatutnya :

 A/2.2( C )'

pening fikir.. script ni sy convert dari vbscript..

code asal strOutput = strOutput & Letter & Right(arySplit(I + 1), Len(arySplit(I + 1)) - 2).

 

Mohon ada yg sudi bantu..terima kasih.

Edited by atomic

Share this post


Link to post
Share on other sites

sy ubah code seperti berikut.. result dapat mcm seperti dikehendaki.. adakah cara ni betul? atau ada cara lebih ringkas?

mohon bantuan..

 

var strConvert = "A%2F2%2E2%28C%29%27";
var arySplit = strConvert.split("%");

if (Object.prototype.toString.call(arySplit) === '[object Array]') {
    for (var i = 0; i < arySplit.length; i++) {
        if (i === 0) {
            strOutput = arySplit[i];
        }
        if (i !== arySplit.length - 1) {
            strHex = "%" + arySplit[i + 1].substring(0, 2);
            Letter = decodeURIComponent(strHex);				
			var strSubstr = "";
			
			if(arySplit[i + 1].length-1 > 1){
				strSubstr += arySplit[i + 1].substring(arySplit[i + 1].length-1);
			};
			strOutput = strOutput + Letter + strSubstr;
        }
    }
alert(strOutput);
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...