English 中文(简体)
字符串上一个上一个索引
原标题:String previous last index
  • 时间:2012-05-28 14:07:25
  •  标签:
  • java
  • csv

有没有一个简单的方法 来得到一个字符串的倒数第二 限定的子字符串?

String original = "/1/6/P_55/T_140";

在此示例中, 产生的子字符串将是 "P_ 55/ T_140"

我想在此子字符串(/ )开头找到前斜线索引。

我知道 < code> string. lastIndexof () 两次打电话会有帮助。 但寻找一种通用的更清洁的方法。 可能对任何 N 来说 。

最佳回答

但寻找一种通用的清洁方法。

拨打String.LastIndexof(int,int) 在循环中将会相当高效,

    int pos = str.length();
    for (int i = 0; i < n; i++) {
        pos = str.lastIndexOf( / , pos - 1);
    }
    String out = str.substring(pos + 1);

这可以很容易地转换为辅助函数, 使用 str , / / < , 并返回 out

问题回答

获取媒体文件所在的文件夹名称

/储存/模拟/0/WhatsApp/Media/ whatsApp 视频/VID-20170812-WA0000.mp4

使用以下代码

String folderName = filePath.substring(filePath.lastIndexOf( / ,filePath.lastIndexOf( / )-1),filePath.lastIndexOf( / ));

返回文件夹Name 作为 whatsApp Vide

要获得上上一个字符串

This - https://developer.co/RTsMlGTsCj/share

To - RTsMlGTsCj

使用此

科特林

val removeDomainName : String = scanResult.substring(
                scanResult.lastIndexOf( / , scanResult.lastIndexOf( / ) - 1),
                scanResult.lastIndexOf( / )
            )
            val profileCode: String = removeDomainName.removePrefix("/")

爪哇

String removeDomainName = scanResult.substring(scanResult.lastIndexOf( / ,scanResult.lastIndexOf( / )-1),scanResult.lastIndexOf( / ));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...