1

have tried various things

split[6].length
String.split[6].length

along these lines without success get this error message for the last one ...

ReferenceError: "string" is not defined.

Hi Thanks for all the replies, in the end I created an array based on the index of the original array and then queried the length of that. As you can see I am having trouble removing single and double quotes from the input strings. New to javascript and its making me a little crazy lol.

// Loop through all the input messages
for (var i = 0; i < input.length; i++) {    
var next = output.append(input[i]);
// Get the body of the current input message
var body = input[i].text;
// Set the body
next.text = body ;
next.text.replace(/\'/g, "&#39;");
next.text.replace(/\"/g, "&#34;");
//replace(/['"]/g,'');
// Set a property    
var split = next.text.split(",");       
var array1 = split[5];
var array2 = split[2];
next.setProperty("aaaLength", split.length);
next.setProperty("aaaSplitValue", split.length);  
next.setProperty("aaaArray1Value", split.length); 
next.setProperty("aaaArray2Value", split.length);

if (next.getProperty("BaseFilename")=="name"){
    next.text.replace(/\'/g, "&#39;");
    next.text.replace(/\"/g, "&#34;");
    //replace(/['"]/g,'');
    if(split.length>10){

        next.setProperty("FullFilename","nameError"+i);
        next.setProperty("BaseFilename","nameError"+i);
        next.setProperty("Suffix",".err");

        }
        if(array1.length>10){
            next.setProperty("FullFilename","nameSnameSuffixError"+i);
            next.setProperty("BaseFilename","nameSnameSuffixError"+i);
            next.setProperty("Suffix",".err");      
        }
}
3
  • What do you mean "length of a specific index"? Do you mean the length of a string stored at that index? Commented Apr 12, 2011 at 2:45
  • please edit your question to include details of the "split" object in your example. Commented Apr 12, 2011 at 2:47
  • We need more surrounding code. What's split supposed to be? Commented Apr 12, 2011 at 2:47

4 Answers 4

2

Length should work if the elements are strings. See the following in action at http://jsfiddle.net/46nJw/

var parts = "foo,bar,baz,foop".split(/,/);
alert( parts[3].length ); // should alert 4
Sign up to request clarification or add additional context in comments.

Comments

0
var arr = ['one','two','three']

arr[1].length

returns 3

1 Comment

I guess I was not very original with my string :/
0

Are you sure it is returning a string?

You can force it to convert to a string like so:

String(split[6]).length;

1 Comment

Hi Thanks for all the replies, in the end I created an array based on the index of the original array and then queried the length of that.
0

I don't know what you need, so I give you all the options I can think of:

var commaSeparatedString = "one, two, three";
var str = commaSeparatedString.split(",");
alert (str.length) // outputs '3'
alert (str[2]); // outputs 'three'
alert (str[2].length); //outputs '5' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.