i need to split a tamil word by character and print it using javascript.
Example :
Input - ஆண்டாள்
output - ஆ
ண்
டா
ள்
can someone help me.
To take each individual character of the string and print it separately you’d want to do:
var myString = "ஆண்டாள்";
var myArray =[];
for(var i = 0; i<myString.length; i++){
myArray.push(myString.charAt(i));
}
//Then print each character however you want example:
for(var i=0; i<myArray.length; i++){
console.log(myArray[i]);
}
If you don't want each character saved in an array you can also do:
var myString = "ஆண்டாள்";
for(var i = 0; i<myString.length; i++){
console.log(myString.charAt(i));
}
Split tamil word by character using javascript...
Input - ஆண்டாள்
Output - ஆ,ண்,டா,ள் (array)... code works 100%
str = "ஆண்டாள்";
var diacritics = {'\u0B82':true,'\u0BBE':true, '\u0BBF':true,
'\u0BC0':true, '\u0BC1':true, '\u0BC2':true, '\u0BC6':true,
'\u0BC7':true, '\u0BC8':true, '\u0BCA':true, '\u0BCB':true,
'\u0BCC':true, '\u0BCD':true, '\u0BD7':true};
var str1 = str.split('');
var Tamil = [];
for(var i = 0; i != str1.length; ++i){
var ch = str1[i];diacritics[ch] ?(Tamil[Tamil.length - 1] +=
ch) : Tamil.push(ch);
}
alert(Tamil);
one more best way is below
str = "ஆண்டாள்";
console.log(str.match(/[\u0b80-\u0bff][\u0bbe-\u0bcd\u0bd7]?/gi));//return ["ஆ", "ண்", "டா", "ள்"]
Related
I need to extract a particular string in javascript of format: “STATE: ip reachable” from another set of string I have as input.
From the extracted string, I need to extract both the numbers.
I have done so far is:
var str=<the input value>;
if(str.contains("STATE"))
{
var str = str.substring(str.indexOf("STATE"), string.indexOf("reachable");
}
I am finding difficulty in how to extract the numbers.
I don't really understand what are the 'numbers', but I tried that :
var str= "lorem ipsum STATE: 10.0.5.2 120.30.66.8 test ... blabla bla";
str = str.slice(str.indexOf("STATE"), str.length);
var tab = str.split(" ");
var num1 = tab[1];
var num2 = tab[2];
Use a regular expression...something like this:
var test = "STATE 10.0.0.18 192.168.42.9 reachable";
var ips = test.match(/(\d+\.\d+\.\d+\.\d+)/g);
for (i = 0; i < ips.length; i++) {
document.write(ips[i]);
document.write("<br>");
}
If you are sure about the pattern in the string, you can use below code :
var str = "STATE 10.0.0.18 192.168.42.9 reachable";
if (str.indexOf("STATE") >= 0) {
var str = str.substring(str.indexOf("STATE ")+6, str.indexOf(" reachable"));
var arrIP = str.split(" ");
for (i = 0; i < arrIP.length; i++) {
alert(arrIP[i]);
}
}
jsfiddle : https://jsfiddle.net/nikdtu/4nx50b81/
I'm working on alternating the case of a string (for example asdfghjkl to AsDfGhJkL).
I tried to do this. I found some code that is supposed to do it, but it doesn't seem to be working.
var str="";
var txt=document.getElementById('input').value;
for (var i=0; i<txt.length; i+2){
str = str.concat(String.fromCharCode(txt.charCodeAt(i).toUpperCase()));
}
Here's a quick function to do it. It makes the entire string lowercase and then iterates through the string with a step of 2 to make every other character uppercase.
var alternateCase = function (s) {
var chars = s.toLowerCase().split("");
for (var i = 0; i < chars.length; i += 2) {
chars[i] = chars[i].toUpperCase();
}
return chars.join("");
};
var txt = "hello world";
console.log(alternateCase(txt));
HeLlO WoRlD
The reason it converts the string to an array is to make the individual characters easier to manipulate (i.e. no need for String.prototype.concat()).
Here an ES6 approach:
function swapCase(text) {
return text.split('').map((c,i) =>
i % 2 == 0 ? c.toLowerCase() : c.toUpperCase()
).join('');
}
console.log(swapCase("test"))
You should iterate the string and alternate between upper-casing the character and lower-casing it:
for (var i=0; i<txt.length; i++) {
var ch = String.fromCharCode(txt.charCodeAt(i);
if (i % 2 == 1) {
ch = ch.toUpperCase();
} else {
ch = ch.toLowerCase();
}
str = str.concat(ch);
}
So the goal of this task is translate english input values into french and vice versa. The problem here is that I don't know how to split the whole input by spaces to get all the words one by one and translate them one by one. Thank you :)
function translateInput(){
for(i = 0; i < ('input').length; i++){
('input').eq(i).val(('value').eq(i).text());
}
}
var translateText = function() {
var translationType = document.getElementById('translation').value;
if (translationType === 'englishToFrench') {
console.log('translation used: English to French');
return 'code1';
}else if(translationType === 'frenchToEnglish'){
console.log('translation used: French to English');
return 'code2';
}else{
return "No valid translation selected.";
}
};
You can use the split function to split the string at its spaces into an array.
var str = YOUR_STRING;
var array = str.split(" ");
http://www.w3schools.com/jsref/jsref_split.asp
Then you can loop through the array and translate word by word.
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
alert(array[i]);
//Translate string
}
Or you can use a Regular Expression, by the way you can practice in a Regex Playground.
var myString = "Hello, my name is JavaScript";
var tokens = a.match(/\w+'?\w*/g); //Assuming you can take words like {"Bonsanto's", "Asus'"}
tokens.forEach(function(word){
console.log(word);
});
My goal is to handle some incoming string that represents a series of hex byte values and output escaped hex values.
I also have the freedom to format the string as I please (add spaces, split into chunks etc).
Example(updated):
var input = "FF655050";
var output = "\xFF\x65\x50\x50";
console.log(output); //ÿePP
I've had no success with string manipulation (append, replace) and I'm not really sure of my options here. I really want to avoid a gigantic switch case.
Edit: sorry for not specifying the output correctly. I want the actual escaped character(in escaped form), not the string representation.
Using a loop:
var input = "FF655050";
var output = "";
for (var i = 1; i < input.length; i+=2) {
output += String.fromCharCode(parseInt(input[i-1] + input[i], 16));
}
alert(output)
Or using regular expressions:
var input = "FF655050";
var output = input.replace(/.{0,2}/g, function(x){ return String.fromCharCode(parseInt(x, 16)) });
alert(output)
Loop through each set of two and prepend "\x" to it
var input = "FF04CA7B";
var i = 0;
var output = "";
while( i < input.length ){
output += "\\x" + input[i];
if(i+1<input.length)output+=input[i+1];
i+=2;
}
alert(output);
A simple for loop incremented by two should do the trick.
var input = "FF04CA7B";
var output = "";
for (var i=1; i<input.length; i+=2) {
output += '\\x'+input[i-1]+input[i];
}
I have a gigantic list (800 items) and one really long string. I want to get the first item in the array that matches the part of the string and stored in a variable.
My code currently:
for (var i = 0; i<gigantic_genre_array.length; i++) {
var test_genre = thelongstr.indexOf(gigantic_genre_array[i]);
if(test_genre != -1) {
tag1 = gigantic_genre_array[test_genre];
alert(tag1);
}
}
This doesn't work like I thought it would, any suggestions?
Try this:
for(var i = 0; i<gigantic_genre_array.length; i++){
var test_genre = thelongstr.indexOf(gigantic_genre_array[i]);
if(test_genre!=-1){
tag1 = gigantic_genre_array[i];
alert(tag1);
}
}
Do the process reversely it will be efficient too.
var wordArray = thelongstr.split(' ');
for(var i=0,len = wordArray.length; i < len; i++)
{
if(gigantic_genre_array.indexOf(wordArray[i]) > -1)
{
alert(wordArray[i]);
}
}
You may create a RegExp based on the array and test it against the string:
var gigantic_genre_array=['foo','bar','foobar'];
var thelongstr='where is the next bar';
alert(new RegExp(gigantic_genre_array.join('|')).exec(thelongstr)||[null][0]);
//returns bar