Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要创建一个接收字符串并检查最后一个字符是否为“\n”的函数。如果是这样,则返回相同的字符串,但没有最后一个字符。我能想到的方法都不是最有效的。我需要它高效。
由于您没有提供任何上下文,因此很难对您的问题给出准确的答案。
我能想到的最简单的解决方案是:
let check s = let n = String.length s in if n > 0 && s.[n-1] = '\n' then String.sub s 0 (n-1) else s