[英]How do I define the end of a string in Java?
我有一個字符串,用戶以[Last,First Middle]格式輸入其名稱,我需要將其更改為[First Middle Last]格式。
我已經定義了last
名字LFM.substring(0, commaSpace)
commaSpace
是LFM
(最后一個,第一中)用戶輸入的輸入中“,”的名稱。
然后我需要定義firstMiddle
。 我對您的問題是,如何定義字符串LFM
的結尾,以便讓firstMiddle
為LFM.substring(commaSpace,
(string of end) );
LFM.substring(commaSpace,
? 這樣,我可以只打印firstMiddle + last
。
我所有的當前代碼:
(真的很混亂,抱歉)
System.out.println();
System.out.println("This program will separate and convert a name in [Last, First, Middle] format to [First Middle Last].");
System.out.println();
System.out.print("Please enter a name in [Last, First Middle] format. ");
Scanner userInput = new Scanner(System.in);
String lineSeparator = System.getProperty("line.separator");
String LFM, first, middle, last, firstMiddle;
int commaSpace, end, lastLength;
userInput.useDelimiter(lineSeparator);
LFM = userInput.nextLine();
commaSpace = LFM.indexOf(",");
last = LFM.substring(0, commaSpace);
lastLength = last.length();
firstMiddle = LFM.substring(commaSpace, //?);
first = LFM.substring(commaSpace + firstMiddle.length());
System.out.println(firstMiddle + (" ") + last);
使用replaceAll
或replaceFirst
函數,因為它接受正則表達式作為第一個參數。
string.replaceAll("^(\\w+),\\s*(\\w+)\\s+(\\w+)$", "$2 $3 $1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.