Add Hyphen After Every Fourth Character in a String Seperated by Comma

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



Leave a reply



Submit