String convert to Int and replace comma to Plus sign
- Use
components(separatedBy:)
to break up the comma-separated string. - Use
trimmingCharacters(in:)
to remove spaces before and after each element - Use
Int()
to convert each element into an integer. - Use
compactMap
(previously calledflatMap
) to remove any items that couldn't be converted toInt
. Use
reduce
to sum up the array ofInt
.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.
var myStr = 'this,is,a,test';var newStr = myStr.replace(/,/g, '-');
console.log( newStr ); // "this-is-a-test"
Python Remove Comma In Dollar Amount
You could use replace
to remove all commas:
"10,000.00".replace(",", "")
Related Topics
How to Use the Appropriate Color Class For the Current Platform
How to Use an Nsattributedstring with a Scrollview in Swiftui
Swift 5: What's 'Escaping Closure Captures Mutating 'Self' Parameter' and How to Fix It
Previewprovider and Observedobject Properties
Multiple Workers in Swift Command Line Tool
Accessing Appstate in Appdelegate with Swiftui's New iOS 14 Life Cycle
In Swift, What Does the ! Symbol Mean in a Function Signature
No Such Module 'Restkit' With Cocoapods and Swift
Why Are Doubles Printed Differently in Dictionaries
Nsfontattributedstring Worked Before Xcode 6.1
Ios13 Navigation Bar Large Titles Not Covering Status Bar
Swift - How to Mutate a Struct Object When Iterating Over It
Take a Snapshot of Current Screen with Metal in Swift
Is Swiftui Backwards-Compatible With iOS 12.X and Older
In Swift 3, How to Calculate the Factorial When the Result Becomes Too High