我有字符串,例如“John Daws Black”与空格分开,我需要将它们分成两部分,这样就会有像“John Daws”这样的名字部分和像“Black”这样的姓氏部分。然而,正面名称部分可以是任何长度,如“John Erich Daws Black”。我的代码可以得到最后一部分:
public String getSurname(String fullName){
String part = "";
for (String retval: fullName.split(" "))
part = retval;
return part;
}
但我不知道如何获得前部。
答案 0 :(得分:8)
找到 last 空间,然后使用$str=file_get_contents('sample.css');
//replace all new lines and spaces
$str = str_replace("\n", "", $str);
$str = str_replace(" ", "", $str);
//write the entire string
file_put_contents('sample.css', $str);
手动拆分。
substring()
答案 1 :(得分:2)
试试这个。
public String getName(String fullName){
return fullName.split(" (?!.* )")[0];
}
public String getSurname(String fullName){
return fullName.split(" (?!.* )")[1];
}
答案 2 :(得分:1)
分割后获取数组的最后一个元素:
function _see(sq, fen, depth, maxDepth, color, chess) {
"use strict";
if (chess.fen() !== fen) {
console.error("s fen/chess sync error");
chess.load(fen);
}
if (chess.in_checkmate() || chess.game_over()) {
return MATE;
} else if (chess.in_check()) {
return 0; // ????
}
var value = 0, moves, index, move_score, tfen, foo, bar;
if (depth < maxDepth) {
moves = chess.moves({
square: sq,
verbose: true
});
if (moves.length > 0) {
counter.seeNodes = counter.seeNodes + 1;
moves = _.chain(moves)
//only captures
.reject(function (e) {
return !e.hasOwnProperty('captured');
})
//material MVV
.sortBy(function (s) {
return evalPiece(s.piece);
})
//captures LVA
.sortBy(function (s) {
return -evalPiece(s.captured);
})
.value();
//counter.sDepth = Math.max(depth, counter.sDepth);
//counter.maxSDepth = Math.max(maxDepth, counter.maxSDepth); console.error(JSON.stringify(moves));
for (index = 0; index < moves.length; index += 1) {
foo = chess.move(moves[index]);
if (foo === null) {
console.error("see move generated error, aborting loop");
break;
}
tfen = chess.fen();
value = Math.max(0, evalPiece(foo.captured) - _see(sq, tfen, depth + 1, maxDepth, -color, chess));
bar = chess.undo();
if (bar === null) {
console.error("see: bar=null");
}
}
}
}
return value;
}
输出:
String fullName= "John Daws Black";
String surName=fullName.split(" ")[fullName.split(" ").length-1];
System.out.println(surName);
修改强> 对于前部,使用子字符串:
Black
输出:
String fullName= "John Daws Black";
String surName=fullName.split(" ")[fullName.split(" ").length-1];
String firstName = fullName.substring(0, fullName.length() - surName.length());
System.out.println(firstName );
答案 3 :(得分:1)
//Split all data by any size whitespace
final Pattern whiteSpacePattern = Pattern.compile("\\s+");
final List<String> splitData = whiteSpacePattern.splitAsStream(inputData)
.collect(Collectors.toList());
//Create output where first part is everything but the last element
if(splitData.size() > 1){
final int lastElementIndex = splitData.size() - 1;
//connect all names excluding the last one
final String firstPart = IntStream.range(0,lastElementIndex).
.mapToObj(splitData::get)
.collect(Collectors.joining(" "));
final String result = String.join(" ",firstPart,
splitData.get(lastElementIndex));
}
答案 4 :(得分:1)
以下方法适合我。它考虑到如果存在中间名,那么它将包含在名字中:
private static String getFirstName(String fullName) {
int index = fullName.lastIndexOf(" ");
if (index > -1) {
return fullName.substring(0, index);
}
return fullName;
}
private static String getLastName(String fullName) {
int index = fullName.lastIndexOf(" ");
if (index > -1) {
return fullName.substring(index + 1 , fullName.length());
}
return "";
}
答案 5 :(得分:0)
只需为上面的 Andreas 答案进行重写,但要使用正确的方法来获取名字和姓氏
public static String get_first_name(String full_name)
{
int last_space_index = full_name.lastIndexOf(' ');
if (last_space_index == -1) // single name
return full_name;
else
return full_name.substring(0, last_space_index);
}
public static String get_last_name(String full_name)
{
int last_space_index = full_name.lastIndexOf(' ');
if (last_space_index == -1) // single name
return null;
else
return full_name.substring(last_space_index + 1);
}
答案 6 :(得分:0)
这是我之前使用过的一种解决方案,它也考虑了后缀。.(末尾3个字符)
/**
* Get First and Last Name : Convenience Method to Extract the First and Last Name.
*
* @param fullName
*
* @return Map with the first and last names.
*/
public static Map<String, String> getFirstAndLastName(String fullName) {
Map<String, String> firstAndLastName = new HashMap<>();
if (StringUtils.isEmpty(fullName)) {
throw new RuntimeException("Name Cannot Be Empty.");
}
String[] nameParts = fullName.trim().split(" ");
/*
* Remove Name Suffixes.
*/
if (nameParts.length > 2 && nameParts[nameParts.length - 1].length() <= 3) {
nameParts = Arrays.copyOf(nameParts, nameParts.length - 1);
}
if (nameParts.length == 2) {
firstAndLastName.put("firstName", nameParts[0]);
firstAndLastName.put("lastName", nameParts[1]);
}
if (nameParts.length > 2) {
firstAndLastName.put("firstName", nameParts[0]);
firstAndLastName.put("lastName", nameParts[nameParts.length - 1]);
}
return firstAndLastName;
}
答案 7 :(得分:0)
尝试一下:
String[] name = fullname.split(" ")
if(name.size > 1){
surName = name[name.length - 1]
for (element=0; element<(name.length - 1); element++){
if (element == (name.length - 1))
firstName = firstName+name[element]
else
firstName = firstName+name[element]+" "
}
}
else
firstName = name[0]