相关疑难解决方法(0)

如何拆分以逗号分隔的字符串?

我有一个长度未知的字符串,看起来像这样

"dog, cat, bear, elephant, ..., giraffe"
Run Code Online (Sandbox Code Playgroud)

将这个字符串除以逗号的最佳方法是什么,这样每个单词都可以成为ArrayList的一个元素?

例如

List<String> strings = new ArrayList<Strings>();
// Add the data here so strings.get(0) would be equal to "dog",
// strings.get(1) would be equal to "cat" and so forth.
Run Code Online (Sandbox Code Playgroud)

java string split

191
推荐指数
4
解决办法
53万
查看次数

如何从字符串的开头和结尾删除换行符(Java)?

我有一个字符串,其中包含一些文本后跟一个空行.保留文本部分的最佳方法是什么,但从末尾删除空白换行符?

java string line blank-line

118
推荐指数
5
解决办法
16万
查看次数

如何删除java中的所有空格

我有一个编程任务,其中一部分要求我创建从用户读取一行的代码并删除该行中的所有空格.该行可以包含一个或多个单词.

我试图用这个程序做的是让它分析每个字符,直到它找到一个空格,然后将该子字符串保存为第一个标记.然后再次循环,直到它不再有令牌或行尾.

我在尝试编译时继续这样做:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index   out of range: 1
    at java.lang.String.charAt(String.java:694)
    at trim.main(trim.java:23)
Run Code Online (Sandbox Code Playgroud)

这是代码

import java.util.Scanner ;
import java.lang.Character;
import java.lang.String ;
public class trim
{
        public static void main (String[]args)
        {

        String a  ;
        String b  ;
        String c ;
        char aChar ;
        int i = 0 ;

        Scanner scan = new Scanner(System.in);

        a = scan.nextLine();
        a =a.trim() ;


         for ( ; i >= 0 ; i++ )
         {
           aChar = a.charAt(i) ;
           if …
Run Code Online (Sandbox Code Playgroud)

java string whitespace

109
推荐指数
8
解决办法
51万
查看次数

使用Scala删除字符串中的空格

我想删除字符串中的空格.

Input: "le ngoc ky quang"  
Output: "lengockyquang"
Run Code Online (Sandbox Code Playgroud)

我尝试了replacereplaceAll方法,但这不起作用.

scala

21
推荐指数
4
解决办法
2万
查看次数

如何从Java中的字符串中删除前导和尾随空格?

我想从字符串中删除前导和尾随空格:

String s = "          Hello World                    ";
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

s == "Hello world";
Run Code Online (Sandbox Code Playgroud)

java whitespace

4
推荐指数
1
解决办法
5万
查看次数

如何从reader.readLine()返回的String中删除unicode空格

我使用reader.readLine()时,字符串长度始终为80个字符,并在主字符串unicode空格填充之后.有没有办法删除那些不需要的字符.(java.io.RandomAccessFile reader)String.trim没有在这方面工作

java android

2
推荐指数
1
解决办法
3650
查看次数

对于Java中的大量输入,解析很长时间失败

我正在尝试将一个非常大的输入读取为String,然后将其转换long为如下所示:[该程序适用于短输入]

输入是两个int用空格分隔的,例如:"1248614876148768372675689568324619856329856295619253291561358926935829358293587932857923857934572895729511 413241"

我的代码:

import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
    public static void main(String args[] ) throws Exception {
        Solution obj = new Solution();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        String input[]=new String[T];
        for (int i=0;i<T;i++) {
            input[i] = br.readLine();
            }
        for (int i=0;i<T;i++){
            StringTokenizer st = new StringTokenizer(input[i]," ");
                BigInteger N = new BigInteger(st.nextToken());
                BigInteger P = new BigInteger(st.nextToken());
                System.out.println(obj.result(N,P));
            }
        }
    }

    public BigInteger result(BigInteger …
Run Code Online (Sandbox Code Playgroud)

java string long-integer

0
推荐指数
1
解决办法
615
查看次数

为了在java中删除String中的空格,我的代码必须是什么样的?

为了在Java中删除String中的空格,我的代码必须是什么样的?

我尝试过以下方法:

/* Option 1 */ String x=splitString.replaceAll("\\s+","");
/* Option 2 */ String x=splitString.trim()
Run Code Online (Sandbox Code Playgroud)

这些都没有给我我期望的结果.

java regex string

0
推荐指数
1
解决办法
2万
查看次数

如何切断String中的所有空格?

我有包含零的字符串,不幸的是有很多空格.我想写一个方法,它将摆脱所有的空格,并返回只有1和0的字符串.

我怎样才能做到这一点?

String example= new String("1 011 01   1");

String shouldReturnStringWithoutWhiteSpaces(String given){
    String cleanString
    //some action which will "transform "1 011 01   1" into "1011011"
    return cleanString;
}
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
641
查看次数

从字符串值中删除空格

http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area在基于Struts的Web应用程序中有一个链接.

URL中的变量在justificationName其vales之前有一些空格,如图所示.当我获得justificationName 使用request.getParameter("justificationName")它的价值时,我会在URL中给出带有空格的值.我想删除这些空格.我试过trim()尝试,str = str.replace(" ", "");但他们中的任何一个都没有删除那些空格.任何人都可以通过其他方式来消除空间.

注意到我做了一件事,右键单击链接并打开链接到新标签,我注意到链接看起来像.

http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName=%A0%A0%A0%A0%A0%A0%A0%A0No%20Technicians%20in%20Area
Run Code Online (Sandbox Code Playgroud)

值得注意的是,它在地址栏中显示的%A0是空白区域,并且还显示%20空间以及链接,并告诉区别,如果有人知道它.

编辑 这是我的代码

String justificationCode = "";
        if (request.getParameter("justificationName") != null) {            
            justificationCode = request.getParameter("justificationName");
        }
        justificationCode = justificationCode.replace(" ", "");
Run Code Online (Sandbox Code Playgroud)

注意:replace函数从字符串内部删除空格但不删除起始空格.例如,如果我的字符串在使用替换后为"This is string",则变为"Thisisstring"

提前致谢

java

-4
推荐指数
1
解决办法
2032
查看次数

标签 统计

java ×9

string ×5

whitespace ×2

android ×1

blank-line ×1

line ×1

long-integer ×1

regex ×1

scala ×1

split ×1