简体   繁体   中英

How to separate the string into two categories in jstl?

i have a string as, String value = "Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14";

Now i need split the names and those values. I am unable to identify what to do.

I need to print the values as

Output
---

-----------------------------
' Name       '  value       ' 
'------------'--------------'
' Bhavani    '  12          '
' Pallavi    '  13          '
' Charanya   '  14          '
'            '              '
----------------------------'

I think it can be done in jstl level.. But can anyone help me how to split that string.

you can use str.replace to create an object:

 strvalue = "Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14" obj={}; strvalue.replace(/(\\w+)<>(\\w+)/g,function(_,m1,m2){ obj[m1]=m2 }) console.log(obj) 

This should do it:

var str = 'Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14';
str = str.split('<<>>')
for(var i = 0; i < str.length; i++) {
  str[i] = str[i].split('<>');
}
alert(str.join("\n"));

first split with <<>> than split wiht <> so you will get array with Bhavani , 12 in two indexes. its simple to show in any way on view.

Personally, I'd store the key-value pairs in an object:

var myObj = {};
var string = 'Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14';
var stringArray = string.split('<<>>');

for(i in stringArray){
    var key = stringArray[i].split('<>')[0];
    var value = stringArray[i].split('<>')[1];
    myObj[key] = value;
}

console.log('My object looks like', myObj);

Here is my solution, you'll get an array of objects with a name and value inside each object:

 let value = "Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14"; let splittedString = value.split("<<>>"); let names = []; splittedString.forEach(function(value) { let splittedProps = value.split("<>"); names.push({ name: splittedProps[0], value: splittedProps[1] }); }); console.log(names); 

var str =  "Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14";
var strArr = str.split("<<>>");
var result = {};
for(var i in strArr){
    var p = strArr[i].split("<>");
    result[p[0]] = p[1];
}

console.log(result);

First split with <<>> then split string with <>

var string = 'Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14';

string = string.split('<<>>');

for(var i = 0; i < string.length; i++) {

string[i] = string[i].split('<>');

}`

 var string = 'Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14'; string = string.split('<<>>'); for(var i = 0; i < string.length; i++) { string[i] = string[i].split('<>'); } alert(string.join("\\n")); 

`

 var str = "Bhavani<>12<<>>Pallavi<>13<<>>Charanya<>14"; var txt = str.split("<<>>").join("|").split("<>").join("|").split("|") var object=[]; for (var i=0; i<txt.length;i=i+2){ object.push({Name:txt[i],Value:txt[i+1]}); } console.log(object); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM