Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a character to the beginning and end of a string

I am using a platform which uses a JavaScript-like syntax and allows to convert my values using regex.

I need to add a single quote at the beginning and at the end of a string, the string will always have 9 characters.

I.e.:

123456789 should be converted to '123456789'

What expression should I use?

like image 370
PepeFloyd Avatar asked Jun 28 '12 14:06

PepeFloyd


People also ask

How do you add a character at the beginning and at the end of a string?

Method 1: By using +: We can add a character to the end or start of a string by using +.

How do you add a character at the beginning of a string?

Example: One can add character at the start of String using the '+' operator.

How do you add a character to the beginning and end of a string in Python?

Add a Character to a String in Python Using the + Operator The + operator can concatenate two strings or a string and a character and returns a new string in Python.

How do I add characters to start and end a string in Java?

Insert a character at the beginning of the String using the + operator. Insert a character at the end of the String using the + operator.


2 Answers

You don't need a regular expression.

str = "123456789";  str = "'" + str + "'"; 
like image 190
evan Avatar answered Oct 03 '22 00:10

evan


Although this question was asked a little early, newly defined backticks standard called Template Literals from ES6 provides new possibility to solve this question with a simpler syntax:

const str = '123456789'; const wrapped = `'${str}'`; 
like image 36
千木郷 Avatar answered Oct 03 '22 02:10

千木郷