How to replace chars and keep string length?

c replace character in string at position
replace character in string c#
c replace character in string at position
replace a character in a string c++
string replace
string replace java
string to char array
replace first character in string java

Is possible in js transform a String in this format

123456789

In something like this

*****6789

I want to use just one statment, something like this but keeping the last four characters.

var a = "12312312312123".replace(/[0-9]/g, "*")
console.log(a)

you could make use of .(?=.{4}) with g flag:

var a = "12312312312123".replace(/.(?=.{4})/g, '*')
console.log(a)

Manipulating Characters in a String (The Java™ Tutorials , You can get the character at a particular index within a string by invoking the The index of the first character is 0, while the index of the last character is length ()-1 . The String class does have four methods for replacing found characters or � The String.Replace method creates a new string containing the modifications. The Replace method can replace either strings or single characters. In both cases, every occurrence of the sought text is replaced. The following example replaces all ' ' characters with '_': string source = "The mountains are behind the clouds today.";

Split the input into substrings, perform the replacement on the first part, and then append the second part.

var input = "123-456-7890";
var prefix = input.substr(0, input.length - 4);
var suffix = input.substr(-4);
var masked = prefix.replace(/\d/g, '*');
var a = masked + suffix;
console.log(a)

Replace every character of a string by a different character , Given a string str of lowercase alphabets only. (small case only) Replacement of str[i] is determined by: for ( int i = 0; i < str.length(); i++) {. 9.4. String Functions and Operators. This section describes functions and operators for examining and manipulating string values. Strings in this context include values of the types character, character varying, and text.

Use padStart

const str = '123456789';
console.log(str.slice(-4).padStart(str.length, '*'));

Or

const str = '123456789';
console.log(str.substr(-4).padStart(str.length, '*'));

substr_replace - Manual, 5, PHP 7). substr_replace — Replace text within a portion of a string If start is negative, the replacing will begin at the start 'th character from the end of string . length. If given and is $chars = $maxlen - $ilen; // number of $str chars to keep std::string has a replace member, but it works in terms of numerical positions, rather than the previous content of the string. As such, you normally have to combine it with the find member in a loop, something like this: std::string old("o"); int pos; while ((pos = x.find(old)) != std::string::npos) x.replace(pos, old.length(), "pppp");

A slightly different approach... by creating two capture groups and replacing the first with * repeated.

const a = '123456789'.replace(/(^\d+)(\d{4}$)/, (m,g1,g2) => '*'.repeat(g1.length) + g2);
console.log(a);

How to modify string contents, The Replace method can replace either strings or single characters. Store the results in a new string variable. var trimmedResult = source. Create(length, chars, (Span<char> strContent, char[] charArray) => { strContent[0]� If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though. If search or replace are arrays, their elements are processed first to last

You can use either the function slice or function substr and the function padStart to fill * from left-to-right.

let str = "1234567893232323232";
console.log(str.slice(5, str.length).padStart(str.length, '*'));

string — CMake 3.12.4 Documentation, Search and Replace string(FIND <string> <substring> <out-var> []) string( REPLACE The following characters have special meaning in regular expressions: ^: Matches at beginning of Store in an output variable a given string's length. Java String replace() The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.. Since JDK 1.5, a new replace() method is introduced, allowing you to replace a sequence of char values.

Strings � The Julia Language, Equal to the number of code units in str multiplied by the size, in bytes, of one code the parent string s within range i:j or r respectively instead of making a copy. julia> unescape_string("aaa \\g \\n", ['g']) # using `keep` argument "aaa \\g \n". Returns the string starting at index position start. The first character in the string is position 1. The first character in the string is position 1. If the optional argument length is added, the returned string includes only that number of characters.

std::string::String, Prevent automatically dropping the String's data let mut story If you need a &str instead of a String , consider str::from_utf8 . we need to insert the replacement characters, which will change the size of the string, and hence, require a String . Since ="" is a string of a known length, you may safely put three ? chars after the $1. If you have no control over the pattern and just need to replace the contents of the first group, and in case you do not know the length of Group 2 value (it is not the case, but let's generalize), you may consider the following approach:

Replace text using regular expression, This MATLAB function replaces the text in str that matches expression with the text described Preserve Case in Original Text Replace Zero-Length Matches. Note that you do not use the new operator to create a string object except when initializing the string with an array of chars. Initialize a string with the Empty constant value to create a new String object whose string is of zero length. The string literal representation of a zero-length string is "".

Comments
  • Awesome. Good job.
  • I don't understand how, but this works amazingly well.
  • This replaces the first 5 characters in the string with '*'. This will fail for strings "123456789012345" or any other strings with length more than 9.