Android去除字符串中空格制表符换行

本文介绍了几种实用的方法来处理字符串中的空白字符(如空格、制表符、换行符等),并提供了一种有效的方式去除数组中的null值。通过具体的代码示例,读者可以学习如何使用循环和正则表达式进行数据清理。

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

两种方法 去除字符串中空格制表符换行:

  public String checkString(String str) {
        if (TextUtils.isEmpty(str)) return "";
        int len = str.length();
        int i = 0, j = 0;
        char[] strChar = str.toCharArray();
        for (; i < len; i++) {
            if (' ' == strChar[i] || '\t' == strChar[i] || '\n' == strChar[i])
                continue;
            if (i != j) strChar[j] = strChar[i];
            j++;
        }
        //strChar[j] = 0;//C/C++中0是结束标志位需要添加该语句,Java中会越界,需要注释该句;
        return new String(Arrays.copyOf(strChar, j));
    }

利用正则表达式

public static String replaceBlank(String src) {
        String dest = "";
        if (src != null) {
            Pattern pattern = Pattern.compile("\t|\r|\n|\\s*");
            Matcher matcher = pattern.matcher(src);
            dest = matcher.replaceAll("");
        }
        return dest;
    }

去除数组中的null

    /**
     * 清空数组中的null
     *
     * @param array 元素组
     * @param <T>   数组元素类型
     * @return 去除null之后的新数组
     */
    public static <T> T[] clearEmptyElements(T[] array) {
        if (array == null || array.length == 0) {
            return null;
        }
        int m = 0, n = 0;
        while (m < array.length) {
            T t = array[m];
            if (t != null) {
                if (m != n) {
                    array[n] = t;
                }
                n++;
            }
            m++;
        }
        return Arrays.copyOf(array, n);
    }
}

补充两种去除的算法

//2.1 Delete
	int k = 0;
	int i = 0;
	while (i < NowListSize)
	{
		if (NowListFlag[i] == 0){
			NowList[i]->deleteIcon();
		}else{
			NowList[k++] = NowList[i];
		}
		i++;
	}
	NowListSize = k;

或者

	int pos = 0;
	for (int j = 0; j < NowListSize; j++){
		if (NowListFlag[j + pos] == 0){
			NowList[j + pos]->deleteIcon();
			pos++;
			NowListSize--;
			j--;
			continue;
		}
		NowList[j] = NowList[j + pos];
	}
按照以下需求输出VBA代码 1.在excel文档所在的路径下,查询到以I开头的aidl文件,将文件里的内容copy到1个新的sheet的A1,以I开头的名称+文件扩展名对sheet进行命名 2.将新sheet里A1单元格的函数名,提取到excel文档sheet1的D列,从D5开始填充,每个函数名一行 3.D列提取的函数,要在对应的C列补充在哪个sheet提取的 4.注意不要下标越界 5.不要使用正则表达式 使用如下代码进行测试,正确的提取的函数结果有allocate、allocate2、isSupported、getIMapperLibrarySuffix。 /* * Copyright 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /////////////////////////////////////////////////////////////////////////////// // THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. // /////////////////////////////////////////////////////////////////////////////// // This file is a snapshot of an AIDL file. Do not edit it manually. There are // two cases: // 1). this is a frozen version file - do not edit this in any case. // 2). this is a 'current' file. If you make a backwards compatible change to // the interface (from the latest frozen version), the build system will // prompt you to update this file with `m <name>-update-api`. // // You must not make a backward incompatible change to any AIDL file built // with the aidl_interface module type with versions property set. The module // type is used to build AIDL files in a way that they can be used across // independently updatable components of the system. If a device is shipped // with such a backward incompatible change, it has a high risk of breaking // later when a module using the interface is updated, e.g., Mainline modules. package android.hardware.graphics.allocator; @VintfStability interface IAllocator { /** * @deprecated As of android.hardware.graphics.allocator-V2 in combination with AIMAPPER_VERSION_5 this is deprecated & replaced with allocate2. If android.hardware.graphics.mapper@4 is still in use, however, this is still required to be implemented. */ android.hardware.graphics.allocator.AllocationResult allocate(in byte[] descriptor, in int count); android.hardware.graphics.allocator.AllocationResult allocate2(in android.hardware.graphics.allocator.BufferDescriptorInfo descriptor, in int count); boolean isSupported(in android.hardware.graphics.allocator.BufferDescriptorInfo descriptor); String getIMapperLibrarySuffix(); }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值