Get first letter of each word in a string, in JavaScript
I think what you're looking for is the acronym of a supplied string.
var str = "Java Script Object Notation";var matches = str.match(/\b(\w)/g); // ['J','S','O','N']var acronym = matches.join(''); // JSON
console.log(acronym)
How can I get the first letter of each word in a string?
[ s[0] for s in 'Piethon is good'.split() ]
Get the first character of each word in a String
Try splitting by " "
(space), then getting the charAt(0)
(first character) of each word and printing it like this:
public static void main(String args[]) {
String x = "Shojibur rahman";
String[] myName = x.split(" ");
for (int i = 0; i < myName.length; i++) {
String s = myName[i];
System.out.println(s.charAt(0));
}
}
Return First Character of each word in a string
I would suggest you to use RegExp
, Here's an Example
var myStr = "John P White";
var matches = myStr.match(/\b(\w)/g);
console.log(matches.join(''));
Get the first letter of each word in a string
explode()
on the spaces, then you use an appropriate substring method to access the first character of each word.
$words = explode(" ", "Community College District");
$acronym = "";
foreach ($words as $w) {
$acronym .= mb_substr($w, 0, 1);
}
If you have an expectation that multiple spaces may separate words, switch instead to preg_split()
$words = preg_split("/\s+/", "Community College District");
Or if characters other than whitespace delimit words (-,_
) for example, use preg_split()
as well:
// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");
First letter of each word in a string and punctuation
If you are using only word characters, you can keep the first character and remove the rest of the word characters.
\B
matches a non word boundary and \w+
matches 1 or more word characters:
const s = "Aliquam ipsum ex, tempus ornare semper ac, varius vitae nibh.";
console.log(s.replace(/\B\w+/g, ""));
How to get the first character of each word in HTML Javascript?
Depends on your definition of word, but this one is reasonable.
[...string.matchAll(/\b\w/g)].join('')
That is, find every word character (\w
) that immediately follows a word boundary (\b
), put them in an array, then join them together.
You should be able to use this expression directly, or implement it as a pipe.
function initials(string) {
return [...string.matchAll(/\b\w/g)].join('')
}
console.log(initials("User 10"))
console.log(initials("The quick brown fox jumps over the lazy dog"))
console.log(initials("Twenty-one. N_x, 4.2"))
How to get the first character of each word in a string?
You can try this code:
let stringInput = "First Last"
let stringInputArr = stringInput.components(separatedBy:" ")
var stringNeed = ""
for string in stringInputArr {
stringNeed += String(string.first!)
}
print(stringNeed)
If have problem with componentsSeparatedByString
you can try seperate by character space and continue in array you remove all string empty.
Hope this help!
Extract first character of each word from the sentence string
Try something like this:
line = "I like to play guitar and piano and drums"
words = line.split()
letters = [word[0] for word in words]
print "".join(letters)
Related Topics
How to Count Query Result Rows
Do Ajax Requests Retain PHP Session Info
How to Pass Arguments to My Function Through Add_Action
How to Convert a String to JSON Object in PHP
How to Pass Get Parameters to Laravel from with Get Method
PHP Cli: How to Read a Single Character of Input from the Tty (Without Waiting for the Enter Key)
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array
How to Get All Captures of Subgroup Matches with Preg_Match_All()
Clean Links to PHP-Generated JavaScript and CSS
Can't Return a Result Set in the Given Context
How to Determine the Memory Footprint (Size) of a Variable
How to Get the Subversion Revision Number in PHP
Include Files from Parent or Other Directory
How to Get Time Zone Through Ip Address in PHP
Executing a PHP Script with a Cron Job