add hyphen after every fourth character in a string seperated by comma
You can use the following regex:
\b\d{4}
with replacement $&-
demo
var num = '1111111111,2222222222,333333333,....'; console.log(num.replace(/\b\d{4}/g, "$&-"));
inserting period after every 3 chars in a string
You can insert a comma after every nth character like this:
>>> my_str = 'qwertyuiopaq'
>>> ','.join(my_str[i:i+3] for i in range(0, len(my_str), 3))
'qwe,rty,uio,paq'
This should work for any arbitrary length of strings too.
Edit: Written as a function in a similar style to @mhawke's answer, with an option to change the grouping/characters.
>>> def f_comma(my_str, group=3, char=','):
... my_str = str(my_str)
... return char.join(my_str[i:i+group] for i in range(0, len(my_str), group))
...
>>> f_comma('qwertyuiopaq')
'qwe,rty,uio,paq'
>>> f_comma('qwertyuiopaq', group=2)
'qw,er,ty,ui,op,aq'
>>> f_comma('qwertyuiopaq', group=2, char='.')
'qw.er.ty.ui.op.aq'
Add separator to string at every N characters?
Regex.Replace(myString, ".{8}", "$0,");
If you want an array of eight-character strings, then the following is probably easier:
Regex.Split(myString, "(?<=^(.{8})+)");
which will split the string only at points where a multiple of eight characters precede it.
How to get the Index of second comma in a string
You have to use code like this.
int index = s.IndexOf(',', s.IndexOf(',') + 1);
You may need to make sure you do not go outside the bounds of the string though. I will leave that part up to you.
Javascript: Returning the last word in a string
Try this:
you can use words with n word length.
example:
words = "Hello World";
words = "One Hello World";
words = "Two Hello World";
words = "Three Hello World";
All will return same value: "World"
function test(words) {
var n = words.split(" ");
return n[n.length - 1];
}
How add separator to string at every N characters in swift?
Swift 5.2 • Xcode 11.4 or later
extension Collection {
func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
sequence(state: startIndex) { start in
guard start < endIndex else { return nil }
let end = index(start, offsetBy: maxLength, limitedBy: endIndex) ?? endIndex
defer { start = end }
return self[start..<end]
}
}
func every(n: Int) -> UnfoldSequence<Element,Index> {
sequence(state: startIndex) { index in
guard index < endIndex else { return nil }
defer { let _ = formIndex(&index, offsetBy: n, limitedBy: endIndex) }
return self[index]
}
}
var pairs: [SubSequence] { .init(unfoldSubSequences(limitedTo: 2)) }
}
extension StringProtocol where Self: RangeReplaceableCollection {
mutating func insert<S: StringProtocol>(separator: S, every n: Int) {
for index in indices.every(n: n).dropFirst().reversed() {
insert(contentsOf: separator, at: index)
}
}
func inserting<S: StringProtocol>(separator: S, every n: Int) -> Self {
.init(unfoldSubSequences(limitedTo: n).joined(separator: separator))
}
}
Testing
let str = "112312451"
let final0 = str.unfoldSubSequences(limitedTo: 2).joined(separator: ":")
print(final0) // "11:23:12:45:1"
let final1 = str.pairs.joined(separator: ":")
print(final1) // "11:23:12:45:1"
let final2 = str.inserting(separator: ":", every: 2)
print(final2) // "11:23:12:45:1\n"
var str2 = "112312451"
str2.insert(separator: ":", every: 2)
print(str2) // "11:23:12:45:1\n"
var str3 = "112312451"
str3.insert(separator: ":", every: 3)
print(str3) // "112:312:451\n"
var str4 = "112312451"
str4.insert(separator: ":", every: 4)
print(str4) // "1123:1245:1\n"
Related Topics
Redirect to Url After Form Submit in React
How to Select Json Response Item With Spaces
React Native: Image Not Displaying With Dynamic/Local Uri
Get Docx File Contents Using Javascript/Jquery
Angular: Toggle Active Class on Only Button the Current Clicked Button (Not Using *Ngfor)
Have a Div Move on Scroll Up and Down
Make Redirect to New Url But Remove Previous from History
How to Display an Image in Two Pages in Pdf Using Jspdf
Cannot Open Local File - Chrome: Not Allowed to Load Local Resource
Mobile Safari Sometimes Does Not Trigger the Click Event
Pdf Blob - Pop Up Window Not Showing Content
Mongodb Get Data to Display in HTML
Reactjs: Expected Onclick Listener to Be a Function, Instead Got Type String
Use Localstorage Variables in PHP
How to Clear Location.State in React-Router on Page Reload