String Convert to Int and Replace Comma to Plus Sign

String convert to Int and replace comma to Plus sign

  1. Use components(separatedBy:) to break up the comma-separated string.
  2. Use trimmingCharacters(in:) to remove spaces before and after each element
  3. Use Int() to convert each element into an integer.
  4. Use compactMap (previously called flatMap) to remove any items that couldn't be converted to Int.
  5. Use reduce to sum up the array of Int.

    let input = " 98 ,99 , 97, 96 "

    let values = input.components(separatedBy: ",").compactMap { Int($0.trimmingCharacters(in: .whitespaces)) }
    let sum = values.reduce(0, +)
    print(sum) // 390

How to convert a string with comma to integer in C#

This Regex will remove all the letters and spaces:

var regex = new Regex(" |[A-z]");
linksOnPage = regex.Replace(linksOnPage, "");

You could use int.Parse and add the NumberStyles.AllowThousands flag:

int num = int.Parse(linksOnPage , NumberStyles.AllowThousands);

Or int.TryParse letting you know if the operation succeeded:

int num;
if (int.TryParse(linksOnPage , NumberStyles.AllowThousands,
CultureInfo.InvariantCulture, out num))
{
// parse successful, use 'num'
}

Or you can also try this:

int num = int.Parse(linksOnPage.Replace(",", ""));

How to replace comma with a dot in the number (or any replacement)

As replace() creates/returns a new string rather than modifying the original (tt), you need to set the variable (tt) equal to the new string returned from the replace function.

tt = tt.replace(/,/g, '.')

JSFiddle

How to replace comma with plus sign using awk

Following will Replace , and ^ (Start of the line) with anything inside "" .In this case + .

awk '{gsub(/,|^/,"+")}1' result.txt

Replace comma if in between numbers with javascript

You can use capturing groups and replace

var text = "Text, 10,10 text, 40 text, 10,60";var nocomma = text.replace(/(\d),(\d)/g, '$1$2');
console.log(nocomma);

JavaScript - Replace all commas in a string

The third parameter of String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it.

The best way is to use regular expression with g (global) flag.