Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used split() to a string  "Haste, Haste, Please Haste, Please Haste to the Mountain" in an array. I have three variables v1, v2, v3 all string type.
I want the result as follows:
V3 = Please Haste to the Mountain
V2 = Please Haste
V1 = Haste, Haste ("Haste, " can be repeated in this substring)
I was able to retrieve V3 and v2 using  jquery but getting a little stuck when it comes to V1.
My array should be like this:

array[0]                                array[1]  array[2]
------------------------------------------------------------------------------
Haste, Haste, Haste,......Haste | Please Haste | Please Haste To The Mountain |
------------------------------------------------------------------------------

Please help
.
Posted
Updated 13-Mar-14 1:08am
v2
Comments
SwarupDChavan 13-Mar-14 7:10am    
you mean haste can be repeated any number of times in first string.
scarletwitch1990 13-Mar-14 7:11am    
yes
scarletwitch1990 13-Mar-14 7:11am    
like the array representation that i have shown
SwarupDChavan 13-Mar-14 7:14am    
you can append the value to V1 by using for loop
scarletwitch1990 13-Mar-14 7:15am    
the limit??

C#
var str = "Haste,Please Haste, Please Haste to the Mountain"
    var arr = str.split(',');

    var V3, V2, V1 = '';

    V3 = arr[3];
    V2 = arr[2];
    for (var i = 0; i < 10; i++) {
        if (i == 0) { V1 += arr[0]; }
        else if (i > 1 && i < 3) { V1 += ',' + arr[0]; }
        else if (i >= 3 && i < 9) { V1 += '.' }
        else  {V1+= arr[0] }
    }
 
Share this answer
 
v3
Comments
scarletwitch1990 13-Mar-14 7:09am    
thanks :)
but i updated my question
SwarupDChavan 13-Mar-14 7:26am    
give this a try
Try this code
C#
var str = "Haste,Please Haste, Please Haste to the Mountain"
    var arr = str.split(',');
    var v1="",v2,v3,temp="";
    arr.foreach(function(item){
    if(item=="Haste"){
    v1=v1+","+item;
    }
    else{
    temp=temp+","+item;
    }
    });
   var arr2=temp.split(',');
   v2=temp[0];
   v3=temp[1];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900