I have string like this: "Welcome Vitalii Mckay "
I need to cut from this string my name and surname, it should left in new string: "Mckay, Vitalii".
But it should be good not just for my name, it should works for other names with different length, for example:
"Welcome John Smith " -> "Smith, John"
or
"Welcome Andrea J. " -> "J., Andrea".
String name = "Welcome Vitalii Mckay";
String[] parts = name.split("\\ ");
name = parts[2] + ", " + parts[1];
Based on #Vishal's answer and OP's comment on #Max's answer, I believe this will work:
String name = " Welcome Vitalii Mckay "; // with spaces in the beginning and in the end
String[] parts = name.trim().split(" "); // you don't really need the \\
name = parts[2] + ", " + parts[1];
Just make sure you trim your String input.
Could you just use a delimiter?
i.e. use a delimiter to separate the three strings, and only print out the two needed values (Surname [2]/Firstname [1])
String s = "Welcome Vitalii Mckay";
String[] split = s.split("\\s+");
System.out.println(split[2] + ", " + split[1]);
// "Welcome"
// followed by the not of space one or more times
// then a space
// followed by anything one or more times
Pattern pattern = Pattern.compile("Welcome ([^ ]+) (.+)");
Matcher matcher = pattern.matcher("Welcome Vitalii Mckay");
if (!matcher.matches()) throw new Exception();
String firstName = matcher.group(1); // groups are captured between ()
String lastName = matcher.group(2); // groups are captured between ()
Related
This question already has answers here:
Remove all occurrences of char from string
(13 answers)
Closed 5 years ago.
I was wondering how to ignore spaces in Java. This program allows you to enter your first, middle and surname which then outputs your initials. I'm now trying to make it ignore any white spaces. Thanks in advance!
String fullName;
char firstName;
char secondName;
char surname;
int space1;
int space2;
System.out.println("Please enter your first name, your second name and your surname: ");
fullName = kybd.nextLine();
firstName = fullName.charAt(0);
space1 = fullName.indexOf(" ");
secondName = fullName.charAt(space1 + 1);
space2 = fullName.lastIndexOf(" ");
surname = fullName.charAt(space2 + 1);
System.out.println("Initials: " + firstName + secondName + surname);
Explanation
You can implicitly ignore them by just removing them from your input text.
Therefore replace all occurrences with "" (empty text):
fullName = fullName.replaceAll(" ", "");
After that call fullName won't contain a whitespace anymore.
However you'll then get a problem with your logic as you split on whitespaces.
Solution
An alternative could be to first trim the text (removing leading and trailing whitespaces). Then do your split and after that you can remove all other whitespaces:
fullName = kybd.nextLine();
// Remove leading and trailing whitespaces
fullName = fullName.trim();
// Bounds
firstSpace = fullName.indexOf(" ");
lastSpace = fullName.lastIndexOf(" ");
// Extract names
String fullFirstName = fullName.substring(0, firstSpace);
String fullSecondName = fullName.substring(firstSpace + 1, lastSpace);
String fullSurname = fullName.substring(lastSpace + 1);
// Trim everything
fullFirstName = fullFirstName.trim(); // Not needed
fullSecondName = fullSecondName.trim();
fullSurname = fullSurname.trim();
// Get initials
firstName = fullFirstName.charAt(0);
secondName = fullSecondName.charAt(0);
surname = fullSurname.charAt(0);
Example
Let's take a look at an example input (_ stands for whitespace):
__John___Richard_Doe_____
We will first trim fullName and thus get:
John___Richard_Doe
Now we identify the first and the last whitespace and split on them:
First name: John
Second name: ___Richard
Surname: _Doe
Last we also trim everything and get:
First name: John
Second name: Richard
Surname: Doe
With charAt(0) we access the initials:
First name: J
Second name: R
Surname: D
More dynamic
Another more dynamic approach would be to merge all successive whitespaces into a single whitespace. Therefore you would need to traverse the text from left to right and start recording once you see a whitespace, end recording if visiting a non-whitespace character, then replace that section by a single whitespace.
Our example then is:
_John_Richard_Doe_
And after an additional trim you can use your regular approach again:
John_Richard_Doe
Or you can use split(" ") and then reject every empty String:
Iterator<String> elements = Pattern.compile(" ").splitAsStream(fullName)
.filter(e -> !e.isEmpty()) // Reject empty elements
.collect(Collectors.toList()) // Collect to list
.iterator() // Iterator
firstName = elements.next().charAt(0);
secondName = elements.next().charAt(0);
surname = elements.next().charAt(0);
Using the example again the Stream first consists of
"", "", "John", "", "", "Richard", "Doe", "", "", "", "", ""
after the filtering it's
"John", "Richard", "Doe"
Minus Sign
As you said you also want
Richard Jack Smith-Adams
output RJS-A, you can simply split on - after splitting on the whitespace.
Pattern spacePatt = Pattern.compile(" ");
Pattern minusPatt = Pattern.compile("-");
String result = spacePatt.splitAsStream(fullName) // Split on " "
.filter(e -> !e.isEmpty()) // Reject empty elements
.map(minusPatt::splitAsStream) // Split on "-"
.map(stream ->
stream.map(e -> e.substring(0, 1))) // Get initials
.map(stream ->
stream.collect(Collectors.joining("-"))) // Add "-"
.collect(Collectors.joining("")); // Concatenate
Which outputs RJS-A.
This approach is a bit more complicated as we need to maintain the information of the sub-streams, we can't just flatMap everything together, otherwise we wouldn't know where to add the - again. So in the middle part we are indeed operating on Stream<Stream<String>> objects.
I think what you're after here is the split method in String
Which you could use like this:
String fullName = "John Alexander Macdonald";
String[] split = fullName.split(" "); // ["John", "Alexander", "Macdonald"]
The other thing you might want is the trim method which removes spaces from the front and the back of a string.
String withSpaces = " a b c ";
String trimmed = withSpace.trim(); // "a b c"
I have below String
string = "Book Your Domain And Get\n \n\n \n \n \n Online Today."
string = str.replace("\\s","").trim();
which returning
str = "Book Your Domain And Get Online Today."
But what is want is
str = "Book Your Domain And Get Online Today."
I have tried Many Regular Expression and also googled but got no luck. and did't find related question, Please Help, Many Thanks in Advance
Use \\s+ instead of \\s as there are two or more consecutive whitespaces in your input.
string = str.replaceAll("\\s+"," ")
You can use replaceAll which takes a regex as parameter. And it seems like you want to replace multiple spaces with a single space. You can do it like this:
string = str.replaceAll("\\s{2,}"," ");
It will replace 2 or more consecutive whitespaces with a single whitespace.
First get rid of multiple spaces:
String after = before.trim().replaceAll(" +", " ");
If you want to just remove the white space between 2 words or characters and not at the end of string
then here is the
regex that i have used,
String s = " N OR 15 2 ";
Pattern pattern = Pattern.compile("[a-zA-Z0-9]\\s+[a-zA-Z0-9]", Pattern.CASE_INSENSITIVE);
Matcher m = pattern.matcher(s);
while(m.find()){
String replacestr = "";
int i = m.start();
while(i<m.end()){
replacestr = replacestr + s.charAt(i);
i++;
}
m = pattern.matcher(s);
}
System.out.println(s);
it will only remove the space between characters or words not spaces at the ends
and the output is
NOR152
Eg. to remove space between words in a string:
String example = "Interactive Resource";
System.out.println("Without space string: "+ example.replaceAll("\\s",""));
Output:
Without space string: InteractiveResource
If you want to print a String without space, just add the argument sep='' to the print function, since this argument's default value is " ".
//user this for removing all the whitespaces from a given string for example a =" 1 2 3 4"
//output: 1234
a.replaceAll("\\s", "")
String s2=" 1 2 3 4 5 ";
String after=s2.replace(" ", "");
this work for me
String string_a = "AAAA BBB";
String actualTooltip_3 = string_a.replaceAll("\\s{2,}"," ");
System.out.println(String actualTooltip_3);
OUTPUT will be:AAA BBB
I have this regex:
String regexPattern = "[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor";
I want to test it against:
String lineString = "8th floor, Prince's Building, 12 Chater Road";
so I do:
boolean isMatching = lineString.matches(regexPattern);
and it return false. Why?
I thought it had something to do with whitespaces in Java, so I removed the whitespace in the regexPattern variable so it reads
regexPattern = "[0-9A-Za-z]+(st|nd|rd|th)floor";
and matched it with a string without white space:
String lineString = "8thfloor,Prince'sBuilding,12ChaterRoad"
it still returns false. Why? Any help very much appreciated.
String.matches() only returns true if the entire string matches the pattern.
Try adding .* to the beginning and end of your regex.
Example:
String regex = ".*[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor.*";
This is not the best approach, however...
Here's a better alternative:
String input = "8th floor, Prince's Building, 12 Chater Road";
String regex = "[0-9A-Za-z]+(st|nd|rd|th)" + " " + "floor";
Pattern p = Pattern.compile(regex);
boolean isMatch = p.matcher(input).find();
If you want to extract the floor number, do this:
String input = "8th floor, Prince's Building, 12 Chater Road";
String regex = "([0-9A-Za-z])+(st|nd|rd|th)" + " " + "floor";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String num = m.group(1);
String suffix = m.group(2);
System.out.println("Welcome to the " + num + suffix + " floor!");
// prints 'Welcome to the 8th floor!'
}
Check out the Pattern API for a boatload of info about Java regular expressions.
Edited, per comments ...
The [0-9A-Za-z]+ part is greedily matching until the end of th.
Try [0-9] instead.
I have an input string that will follow the pattern /user/<id>?name=<name>, where <id> is alphanumeric but must start with a letter, and <name> is a letter-only string that can have multiple spaces. Some examples of matches would be:
/user/ad?name=a a
/user/one111?name=one ONE oNe
/user/hello?name=world
I came up with the following regex:
String regex = "/user/[a-zA-Z]+\\w*\\?name=[a-zA-Z\\s]+";
All of the above examples match the regex, but it only looks at the first word in <name>. Shouldn't the sequence \s allow me to have white spaces?
The code that I made to test what it is doing is:
String regex = "/user/[a-zA-Z]+\\w*\\?name=[a-zA-Z\\s]+";
// Check to see that input matches pattern
if(Pattern.matches(regex, str) == true){
str = str.replaceFirst("/user/", "");
str = str.replaceFirst("name=", "");
String[] tokens = str.split("\\?");
System.out.println("size = " + tokens.length);
System.out.println("tokens[0] = " + tokens[0]);
System.out.println("tokens[1] = " + tokens[1]);
} else
System.out.println("Didn't match.");
So for example, one test might look like:
/user/myID123?name=firstName LastName
size = 2
tokens[0] = myID123
tokens[1] = firstName
whereas the desired output would be
tokens[1] = firstName LastName
How can I change my regex to do this?
Not sure what you think is the problem in your code. tokens[1] will indeed contain firstName LastName in your example.
Here's an ideone.com demo showing this.
However, have you considered using capturing groups for the id and the name.
If you write it like
String regex = "/user/(\\w+)\\?name=([a-zA-Z\\s]+)";
Matcher m = Pattern.compile(regex).matcher(input);
you can get hold of myID123 and firstName LastName through m.group(1) and m.group(2)
I don't find any fault in your code but you may capture group like this:
String str = "/user/myID123?name=firstName LastName ";
String regex = "/user/([a-zA-Z]+\\w*)\\?name=([a-zA-Z\\s]+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(m.group(1) + ", " + m.group(2));
}
The problem is that * is greedy by default (it matches the whole string), so you need to modify your regex by adding a ? (making it reluctant):
List<String> str = Arrays.asList("/user/ad?name=a a", "/user/one111?name=one ONE oNe", "/user/hello?name=world");
String regex = "/user/([a-zA-Z]+\\w*?)\\?name=([a-zA-Z\\s]+)";
for (String s : str) {
Matcher matcher = Pattern.compile(regex).matcher(s);
if (matcher.matches()) {
System.out.println("user: " + matcher.group(1));
System.out.println("name: " + matcher.group(2));
}
}
Output:
user: ad
name: a a
user: one111
name: one ONE oNe
user: hello
name: world
I'm not new to Java, but have not dealt with Regex and Patterns before. What I'm looking to do is take a string like
"Class: " + data1 + "\nFrom: " + data2 + " To: " + data3 + "\nOccures: " + data4 + " In: " + data5 + " " + data6;
and pull out only data_1 to data_n.
I appreciate any help.
Use this regex:
Pattern pattern = Pattern.compile("Class: (.+?)\nFrom: (.+?) To: (.+?)\nOccures: (.+?) In: (.+?) (.+?)");
Matcher matcher = pattern.matcher(yourInputString);
if (matcher.find())
{
String data1 = matcher.group(1);
String data2 = matcher.group(2);
String data3 = matcher.group(3);
String data4 = matcher.group(4);
String data5 = matcher.group(5);
String data6 = matcher.group(6);
} else
{
// String didn't match the specified format
}
Explanation:
.+? will match any character for undefined times, but non-greedy.
(), using brackets will create a group. A group is given an index starting by 1 (since group 0 is the entire match)
So, (.+?) will creates groups of any character.
And what the matcher does, is searching for the whole pattern somewhere in the input string. But since you specified the format, we know exactly how your entire string is going to look like. The only thing you have to do is copy the format and replace the data you want to extract with "something" (.+?), which you give an index by creating a group of it.
Afterwards, the matcher will try to find the pattern (done by matcher.find()) and you ask them what the content is of the groups 1 up to 6.
how about using split() with ":", then from the splitted String[] get string[2i+1] ? (i from 0)