在Java

时间:2017-06-05 10:40:10

标签: java string

我想在Java中将每行句子分成一行。

输入字符串: “由于投资者权衡了美国总统大选的潜在影响,加强了经济状况和利率上升,”基金会财政年度上半年市场债券市场出现波动。市场受到创纪录的市政债券压力的影响在此背景下,所有六只基金均下跌,从美国基金短期免税债券基金的-0.92%降至美国高收入市政债券基金的-3.77%(见第4至10页)。资助具体的结果和信息。)“

输出:

判决1:由于投资者权衡了美国总统大选的潜在影响,加强了经济状况和利率上升,因此在基金财政年度的上半年,市场债券市场出现波动。

判刑2:市场受2016年市政债券发行创纪录水平的进一步压力。在此背景下,所有六只基金均录得跌幅,从美国基金短期免税债券基金-0.92%至-3.77不等美国高收入市政债券基金的百分比。

Sentence3 :(有关基金的具体结果和信息,请参见第4至10页。

我写了一个java代码来分解句子。('完全停止')发生了,美国之后出现了新的一条线。

string = string.replace(“。”,“。\ n”)

3 个答案:

答案 0 :(得分:1)

您可以将String::split与正则表达式一起使用来完成此操作:

String[] sentences = paragraph.split("(?<=[^ ]\\.) (?=[^a-z])");
int count = 0;
for(String str:sentences)
    System.out.println("Sentence " + (++count) + ":" + str);

这使用了名为look ahead的高级正则表达式技术,并在匹配时保留了分隔符。

答案 1 :(得分:0)

String#split()采用正则表达式。在正则表达式中,.表示\n以外的任何内容。使用\转义点,结果参数变为\\.

答案 2 :(得分:0)

在代码中尝试类似的内容:

List<String> eachLine = new ArrayList<String>();
String initialString = new String("Volatility returned to the municipal bond market during the first half of the funds’ fiscal year as investors weighed the potential impact of the U.S. presidential election, strengthening economic conditions and rising interest rates. The market was further pressured by a record level of municipal bond issuance in 2016. Against this backdrop, all six funds registered declines, ranging from –0.92% for American Funds Short-Term Tax-Exempt Bond Fund to –3.77% for American High-Income Municipal Bond Fund. (See pages 4 through 10 for fund specific results and information.)");

int stopIndex = initialString.indexOf( '. ' );//I am searching for the first occurance of '. ' in the string. 
//Note full stop followed blank space, which would denote either end of a sentence or words like U.K. or U.S. etc.

boolean UpperCase = checkForUpperCase(stopIndex+1);//write a function to check whether the alphabet/character following '. ' is in uppercase or not
//checking for Uppercase because a senetence starts with Uppercase
if(UpperCase){
   eachLine.add(initialString.substring(0,stopIndex));//add the sentence to List<String> to be processed later
   initialString = initialString.substring(stopIndex+1);//storing the rest of the sentence in the same string to be processed again
}
//keep parsing till you parse the whole string

您可以从这里了解有关如何检查大写字母的一般信息:Java Program to test if a character is uppercase/lowercase/number/vowel

上述代码只是一个片段,可让您了解如何实现目标或解决问题。

您也可以使用正则表达式来查找完整停止模式,但了解基本方法可能会在以后更有用。

Java中的正则表达式:https://www.tutorialspoint.com/java/java_regular_expressions.htm