Add Space After Every 4Th Character

How to Add space between every 4 characters in JavaScript?

You can use RegEx for this

let dummyTxt='1234567890123456';
let joy=dummyTxt.match(/.{1,4}/g);console.log(joy.join(' '));

How to insert space every 4 characters for IBAN registering?

The existing answers are relatively long, and they look like over-kill. Plus they don't work completely (for instance, one issue is that you can't edit previous characters).

For those interested, according to Wikipedia:

Permitted IBAN characters are the digits 0 to 9 and the 26 upper-case Latin alphabetic characters A to Z.

Here is a relatively short version that is similar to the existing answers:

document.getElementById('iban').addEventListener('input', function (e) {  e.target.value = e.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();});
<label for="iban">iban</label><input id="iban" type="text" name="iban" />

How to: add a blank space after every 4 characters when typing in TextFormField

I also faced this problem before and luckily found a way to do this somewhere.
First create a class that extends TextInputFormatter

customInputFormatter.dart

class CustomInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
var text = newValue.text;

if (newValue.selection.baseOffset == 0) {
return newValue;
}

var buffer = new StringBuffer();
for (int i = 0; i < text.length; i++) {
buffer.write(text[i]);
var nonZeroIndex = i + 1;
if (nonZeroIndex % 4 == 0 && nonZeroIndex != text.length) {
buffer.write(' '); // Replace this with anything you want to put after each 4 numbers
}
}

var string = buffer.toString();
return newValue.copyWith(
text: string,
selection: new TextSelection.collapsed(offset: string.length)
);
}
}

And then add it to the list of inputFormatters[] of TextFormField

inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
new CustomInputFormatter()
],

Add a space every 4 characters in input with a Pipe

The issue here is you are actually no adding any space in your regex. Instead, you are replacing the text again with same value. Also, you are not updating the value with replaced value. You are just returning the current value only like:

function transform(value) {  if (value != null) {    value.replace(/[^\dA-Z]/g, '')      .replace(/(.{4})/g, value)      .trim();    console.log(value);  }  return value;}
transform('123456789') //=> 123456789 ... returns same value

PHP Add Space Every 4th Character but ignore when you reach a comma

Explode your string, then loop through it and use chunk_split to place the space every fourth character.

$string = 'AQUA19097444,AQUA43188766,AQUA49556282,AQUA51389151,AQUA57267110,BLUE12811521,BLUE15966728';

$exploded_string = explode(',', $string);

foreach($exploded_string as $item){
$result[] = chunk_split($item, 4, ' ');
}

print_r($result); will give you

Array ( [0] => AQUA 1909 7444 
[1] => AQUA 4318 8766
[2] => AQUA 4955 6282
[3] => AQUA 5138 9151
[4] => AQUA 5726 7110
[5] => BLUE 1281 1521
[6] => BLUE 1596 6728 )

If you want to convert this back into a comma separated string use $result = implode(',', $result);

Add a space between every 2 characters starting from the end of a string

simple use strrev() and chunk_split()

<?php

$str = 9010201;

echo trim(strrev(chunk_split(strrev($str),2, ' ')));

?>


Related Topics



Leave a reply



Submit