I'm trying to get the first letter of each word in a string using regex, here is what I have tried:
public class Test
{
public static void main(String[] args)
{
String name = "First Middle Last";
for(String s : name.split("(?<=[\\S])[\\S]+")) System.out.println(s);
}
}
The output is as follows:
F
M
L
How can I fix the regex to get the correct output?
To get the first letter of each word in a string: Call the split() method on the string to get an array containing the words in the string. Call the map() method to iterate over the array and return the first letter of each word. Join the array of first letters into a string, using the join() method.
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
Get the first character of a string in python As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e. It returned a copy of the first character in the string. You can use it to check its content or print it etc.
The regular expression to match String which contains a digit as first character is “^[0-9]. *$”. Pass this as a parameter to the matches() method of the String class.
It is here: How to Get First Letter of every word in a String As-is, that code will return an array of strings, where each string is just the first character. If you want it to be a single string at the end, then just append a .Join () at the end of it Thank you Dave! var106DeathCity.Split (" "c).
Get the first letter of each word in a string using regex in Java. Given a string, extract the first letter of each word in it. “Words” are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z.
The regex works by serching for single word characters (\w) that have one or more space characters (\s+) or are at the start of a line (^). To modify what is being searched for, the \w can be changed to other regex valid entries. To modify what precedes the search character, modify (\s+|^).
Hi @aliaga try like this first split the string using space and then you will get array of words you will get once you get the word then use substring to get the first character from that word and keep looping that words. Try with the following code, "First Name of string".Split (" "c). [Select] (Function (x) x.ToUpper.First ()).ToArray ()
Edit Took some suggestions in the comments, but kept the \S
because \w
is only alpha-numeric and might break unexpectedly on any other symbols.
Fixing the regex and still using split:
name.split("(?<=[\\S])[\\S]*\\s*")
Why not simply:
public static void main(String[] args)
{
String name = "First Middle Last";
for(String s : name.split("\\s+")) System.out.println(s.charAt(0));
}
(Disclaimer: I have no experience with Java, so if it handles regexes in ways that render this unhelpful, I apologize.)
If you mean getting rid of the spaces preceding the M and L, try adding optional whitespace at the end
(?<=[\\S])[\\S]+\\s*
However, this may add an extra space in the case of single-letter words. This may fix that:
(?<=[\\S])[\\S]*\\s*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With