Android关于String数据的startsWith()和endsWith()函数操作

本文介绍了Java中用于字符串前缀和后缀判断的方法startsWith()和endsWith()。通过源码解析了这两个方法的工作原理,并提供了多个示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文链接:http://blog.csdn.net/qq_16628781/article/details/49337619   


知识点:

1、Java函数startsWith()简介及用法;

2、Java函数endsWith()简介及用法;


有很多时候我们需要某个字串进行前缀或者是后缀的判断,例如在webView中加载一个网页,但是不是我们所要的那个网页,或者是进行某些字段的“拦截”功能,依据判断前缀或者是后缀来归类文件,文件夹等等操作。

   下面来介绍两个常用到的函数,他们都是String类里面的函数,直接new一个对象就可以调用了,或者是直接一个字串.就可以调用了。不多说了,先从源码的角度解释函数。

      

 /**
     * Compares the specified string to this string to determine if the
     * specified string is a prefix.
     *
     * @param prefix
     *            the string to look for. 给定的字串
     * @return {@code true} if the specified string is a prefix of this string,
     *         {@code false} otherwise 返回布尔值
     * @throws NullPointerException
     *             if {@code prefix} is {@code null}.
     */
//解释:给定的字串和自己定义的字串相比较,看是否是前缀相同。eg:A.startsWith(B),A是给定的
//B是自己定义的字串
    public boolean startsWith(String prefix) {
        return startsWith(prefix, 0);
    }


当然它并不孤单,还有一个同父异母的长兄:

   

 /**
     * Compares the specified string to this string, starting at the specified
     * offset, to determine if the specified string is a prefix.
     *
     * @param prefix
     *            the string to look for.
     * @param start
     *            the starting offset.
     * @return {@code true} if the specified string occurs in this string at the
     *         specified offset, {@code false} otherwise.返回布尔值
     * @throws NullPointerException
     *             if {@code prefix} is {@code null}.
     */
//解释:指定字串和定义的字串从指定的start位置开始比较是否前缀相同。
//eg:A.startsWith(B,C),看A是否和B从C位置开始是否前缀相同,A为指定字串,B为定义字串
    public boolean startsWith(String prefix, int start) {
        return regionMatches(start, prefix, 0, prefix.count);
    }



示例eg:

        String str1 = "this is str1";
        String str2 = "this is str2";
        if (str1.startsWith(str2)) {
            System.out.println("===>>yes=" + str1.startsWith(str2));
        } else {
            System.out.println("===>>no=" + str1.startsWith(str2));
        }
//结果:===>>no=false 最后的字符不同,空格也表示相同


        String str1 = "this is str1";
        String str2 = "this i";
        if (str1.startsWith(str2)) {
            System.out.println("===>>yes=" + str1.startsWith(str2));
        } else {
            System.out.println("===>>no=" + str1.startsWith(str2));
        }
//结果是:===>>yes=true ,空格而是算前缀的一个,有空格处也相等。


        String str1 = "this is str1";
        String str2 = "t";
        if (str1.startsWith(str2)) {
            System.out.println("===>>yes=" + str1.startsWith(str2));
        } else {
            System.out.println("===>>no=" + str1.startsWith(str2));
        }

//结果:===>>yes=true ,同样一个字母t也是算共同的浅醉;

	
        String str1 = "this is str1";
        String str2 = "tH";
        if (str1.startsWith(str2)) {
            System.out.println("===>>yes=" + str1.startsWith(str2));
        } else {
            System.out.println("===>>no=" + str1.startsWith(str2));
        }
//结果:===>>no=false 看,它还区分大小写。

   最后,还没有完,我们看了前缀的比较,那有没有后缀做比较的呢?答案是有的。我们来看看。

    /**
     * Compares the specified string to this string to determine if the
     * specified string is a suffix.
     *
     * @throws NullPointerException
     *             if {@code suffix} is {@code null}.
     */
//解释:比较指定字串和定义字串的后缀是否相等。返回布尔值类型。
    public boolean endsWith(String suffix) {
        return regionMatches(count - suffix.count, suffix, 0, suffix.count);
    }

   因为这个和startsWith()用法差不多,这里就不给出示例了,大家可以多去尝试下。

   谢谢大家!



### C++递推的实现方法 #### 1. 使用简单循环结构实现递推 对于许多简单的递推问题,可以直接利用循环结构完成。例如斐波那契数列是一个经典的递推例子。 ```cpp #include <iostream> using namespace std; int fibonacci(int n) { if (n <= 1) return n; int a = 0, b = 1, c; for (int i = 2; i <= n; ++i) { c = a + b; a = b; b = c; } return b; } int main() { int n = 10; cout << "Fibonacci number at position " << n << ": " << fibonacci(n) << endl; return 0; } ``` 上述代码展示了如何通过循环计算第 `n` 个斐波那契数[^2]。 #### 2. 利用递归函数实现递推 递归是一种自然的方式来表达递推关系。下面展示了一个基于递归的斐波那契数列实现: ```cpp #include <iostream> using namespace std; int fib_recursive(int n) { if (n <= 1) return n; return fib_recursive(n - 1) + fib_recursive(n - 2); } int main() { int n = 10; cout << "Fibonacci number at position " << n << ": " << fib_recursive(n) << endl; return 0; } ``` 尽管这种方法简洁明了,但它的时间复杂度较高 \(O(2^n)\),因此不适用于较大的输入值。 #### 3. 动态规划解决递推问题 动态规划提供了一种高效的方式存储中间状态并减少冗余计算。以下是对斐波那契序列的一种改进版本: ```cpp #include <iostream> #include <vector> using namespace std; int fib_dp(int n) { vector<int> dp(n + 1, 0); // 初始化dp数组 if (n >= 1) dp[1] = 1; for (int i = 2; i <= n; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } int main() { int n = 10; cout << "Fibonacci number at position " << n << ": " << fib_dp(n) << endl; return 0; } ``` 此方法的空间复杂度为 \(O(n)\),时间复杂度降为线性级别 \(O(n)\)[^4]。 #### 4. 高效空间优化版动态规划 如果只需要最终的结果而不需要保存整个历史记录,则可以进一步降低空间需求至常量级 \(O(1)\): ```cpp #include <iostream> using namespace std; long long fib_optimized(int n) { if (n <= 1) return n; long long prev2 = 0, prev1 = 1, current; for (int i = 2; i <= n; ++i) { current = prev1 + prev2; prev2 = prev1; prev1 = current; } return prev1; } int main() { int n = 50; cout << "Fibonacci number at position " << n << ": " << fib_optimized(n) << endl; return 0; } ``` 这种优化方式特别适合于处理大规模数据集的情况。 --- ### §相关问题§ 1. 如何在 C++ 中使用母函数解决复杂的递推关系? 2. 在实际项目开发过程中遇到性能瓶颈时,有哪些策略可用于优化递归算法的表现? 3. 如果需要支持超大整数运算,在标准库之外还需要引入哪些第三方工具或扩展? 4. 杨辉三角与二项式定理之间存在怎样的联系?能否给出具体的 C++ 实现案例? 5. 对比分析不同类型的背包问题及其对应的解决方案特点是什么?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值