编程语言
首页 > 编程语言> > java-如何从URL提取ID?谷歌表

java-如何从URL提取ID?谷歌表

作者:互联网

我有以下网址.

https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/a/example.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY

对于每个网址,我需要提取工作表ID:1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY到Java字符串中.

我正在考虑使用split,但不能在所有测试用例中使用:

String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
String[] parts = string.split("/");
String res = parts[parts.length-2];
Log.d("hello res",res );

我怎么可能呢?

解决方法:

如果您可以将代码更新为该ID,则看起来您所寻找的ID始终遵循“ / spreadsheets / d /”

        String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
        String[] parts = string.split("spreadsheets/d/");
        String result;
        if(parts[1].contains("/")){
            String[] parts2 = parts[1].split("/");
            result = parts2[0];
        }
        else{
            result=parts[1];
        }
        System.out.println("hello "+ result);

标签:url,google-sheets-api,string,java
来源: https://codeday.me/bug/20191025/1931338.html