Replace a character at a specific index in a string?

String are immutable in Java. You can’t change them. You need to create a new string with the character replaced. String myName = “domanokz”; String newName = myName.substring(0,4)+’x’+myName.substring(5); Or you can use a StringBuilder: StringBuilder myName = new StringBuilder(“domanokz”); myName.setCharAt(4, ‘x’); System.out.println(myName);

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

I use a for loop to iterate the string and use charAt() to get each character to examine it. Since the String is implemented with an array, the charAt() method is a constant time operation. String s = “…stuff…”; for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); //Process char } … Read more

How can I remove a character from a string using JavaScript?

var mystring = “crt/r2002_2″; mystring = mystring.replace(‘/r’,”https://stackoverflow.com/”); will replace /r with / using String.prototype.replace. Alternatively you could use regex with a global flag (as suggested by Erik Reppen & Sagar Gala, below) to replace all occurrences with mystring = mystring.replace(/\/r/g, “https://stackoverflow.com/”); EDIT: Since everyone’s having so much fun here and user1293504 doesn’t seem to be … Read more

Get nth character of a string in Swift programming language

Attention: Please see Leo Dabus’ answer for a proper implementation for Swift 4 and Swift 5. Swift 4 or later The Substring type was introduced in Swift 4 to make substrings faster and more efficient by sharing storage with the original string, so that’s what the subscript functions should return. Try it out here extension … Read more

What characters are valid for JavaScript variable names?

To quote Valid JavaScript variable names, my write-up summarizing the relevant spec sections: An identifier must start with $, _, or any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”. The rest of the string can contain the same … Read more

Repeat a string in JavaScript a number of times

These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write: “a”.repeat(10) Before repeat, we used this hack: Array(11).join(“a”) // create string with 10 a’s: “aaaaaaaaaa” (Note that an array of length 11 gets you only 10 “a”s, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)