Invalid expression term '[' using c# [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone assist me what's wrong in my code. I'm try to convert JavaScript code to C# code
public class SplitString
{
public static string[] Solutions(string str)
{
arr = [];
for(var i = 0; i < str.Length; i += 2){
second = str[i+1] || '_';
arr.push(str[i] + second);
}
return arr;
}
}
And i encounter this error
src/Solution.cs(5,11): error CS1525: Invalid expression term '['
src/Solution.cs(5,12): error CS0443: Syntax error; value expected

javascript and C# are completely different languages; .NET / C# arrays are fixed size - so, you might want a list here:
var arr = new List<string>();
for(var i = 0; i < str.Length; i += 2){
var second = str[i+1]; // || '_'; <== this on the right makes no sense in C#
arr.Add(str[i] + second);
}
return arr.ToArray();

Related

How can I replace the text between two delimiters with a blank? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a message, received via an input field. For example:
This is *red* colour.
I want to replace everything between the two asterisks with a blank line ("___") so the outcome would be:
This is ___ colour.
How can I achieve this?
function myFunction() {
var str = "This is *red* color";
var startIndex = nthIndex(str,'*',1);
var endIndex = nthIndex(str,'*',2);
var output = str.replace(str.substring(startIndex, (endIndex+1)), "_");
}
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}

How can I do this using Reduce function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
var y= '110001'.split("").reverse();
var sum = 0;
for (var i = 0; i < y.length; i++) {
sum += (y[i] * Math.pow(2, i));
}
console.log(sum);
It would be simplest to do
console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur));
<< is the left-bitshift operator, which here essentially multiplies by two.
Array.from (if available) is preferable to split. In this case it doesn't matter, but split will fail with surrogate pair characters such as 🍺, while Array.from will handle them correctly. This could also be written as [...'110001'], which ends up being the same thing.
Of course, you could also just say
parseInt('110001', 2)
check this snippet
var binary = '110001'.split("").reverse();
var sum = binary.reduce(function(previous, current, index) {
previous = previous + (current * Math.pow(2, index));
return previous;
}, 0);
console.log(sum);
Hope it helps

Addition returns wrong value in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I tried with additions of variables I saw that:
https://jsfiddle.net/tyfyLsw9/
I think it's because this doesn't contain an integer.
var month = $("#monthd").val();
var J = 1;
var D = 8;
var K = J + D;
var U = J + month;
As you can see in fiddle J + month returns 110 instead of 11, why?
its a string, so the number you are adding gets coerced into a string as well. "10" + "1" = "101";
simply wrap the value returned in a Number Construct
var month = Number($("#monthd").val());
additionally you can use parseInt if the values are integers.
var month = parseInt($("#monthd").val(), 10);
the , 10 is important to parse it with base 10.

how to find no. of similar characters in two strings in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to find the no. of similar characters between two strings using JS/PHP
Example
str1: Mack
str2: Michelle
Similar Characters: "M" "C"
similar character count: 2
I'll do this:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0;
var min = Math.min(str1.length, str2.length);
for(var i = 0; i < min; i++)
{
if(str1.charAt(i) === star2.charAt(i))
counter++;
}
alert("Result: "+counter);
After your precision, here is my solution:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0, find = -1;
for(var i = 0; i < str1.length; i++)
{
find = str2.indexOf(str1.charAt(i));
if(find > -1)
{
counter++;
str2 = str2.substr(0, find) + str2.substr(find + 1);
}
}
alert("Result: "+counter);
It works with the 2 examples you gave us:
Michellle and Michelle = 8
Sneha and naveen = 3
Use similar_text() function
<?php
echo "character count: ". similar_text("Mack","Michelle");
?>
Output
character count: 2
I'd need more examples to make sure this works, but using regular expressions might work for you:
function similar_text_regex(str1, str2){
var regEx = new RegExp('['+str2+']', 'gi');
var matchCnt = str1.match(regEx2).length;
return matchCnt;
}
console.log(similar_text_regex('Michelle', 'Michellle')); // --> 8
console.log(similar_text_regex('Jason', 'Jane')); // --> 3
console.log(similar_text_regex("sneha","naveen")); // --> 3

How to find number inside [] Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following syntax.
var name = [Name_is][234]
var number = find [234];
How can i find this number in javascript/jquery which is inside [] ?
Use a regular expression.
var string = "[Name_is][234]"
var matches = string.match(/\[(\d+)]/);
if (matches.length) {
var num = matches[1];
}
Check out a working fiddle: http://jsfiddle.net/rEJ5V/, and read more on the String.match() method.
if you want to extract the text between the [ ], you can do:
var name = "[Name_is][234]";
var check= "\{.*?\}";
if (name.search(check)==-1) { //if match failed
alert("nothing found between brackets");
} else {
var number = name.search(check);
alert(number);
}

Categories