简体   繁体   English

如何在不使用库 function 的情况下删除 java 中字符串之间的空格?

[英]how to remove spaces in between string in java,without using library function?

I am getting the solution is as follows我得到的解决方案如下

class StringCheck
{
    public static void main (String[] args) 
    {   
        String str="Hello world I am here";
        String r;

        System.out.println(str);    
        r = str.replaceAll(" ","");
        System.out.println(r);  
    }
}
OUTPUT: HelloworldIamhere

but I don't want to use a library function ie str.replaceAll(), Can anyone help me with the program.I want same output as I got using library function但我不想使用库 function 即 str.replaceAll(),任何人都可以帮助我完成该程序。我想要与使用库 function 相同的 output

What I understood from your question is you don't want to use str.replaceAll() function.我从您的问题中了解到您不想使用str.replaceAll()函数。 So possible alternative is following.所以可能的替代方案如下。 Please refer more details Removing whitespace from strings in Java请参阅更多详细信息在 Java 中从字符串中删除空格

import java.util.*;

public class StringCheck {
    public static void main(String[] args) {
        String str = "Hello world I am here";
        String r = "";

        Scanner sc = new Scanner(str);

        while(sc.hasNext()) {
            r += sc.next();
        }
        System.out.println(r);
    }
}

Well as you do not want to keep the value just loop and print好吧,因为您不想保留该值,只是循环并打印

    String str="Hello world I am here";

    for (char c : str.toCharArray()) {
        if (c != ' ')
            System.out.print(c);
    }

output输出

HelloworldIamhere你好世界我在这里

Of course if you wanted to keep this new String then use a StringBuilder and append the char instead of/and print it当然,如果你想保留这个新字符串,那么使用 StringBuilder 并append字符而不是/并打印它

Iterate each char in str .迭代str每个字符。

If it is not equal ' ', append it to your result string r .如果它不等于 ' ',请将其附加到您的结果字符串r

But why would you want to do it without using library functions?但是你为什么要在不使用库函数的情况下做到这一点呢?

public class Removespaces {

    public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     String s = sc.nextLine();
     char[] ch = s.toCharArray();
     String s1 = "";
     for(int i=0;i<ch.length;i++){
         if(s.charAt(i)==' ')
         {
                 continue;
         }else{
             s1 = s1 + s.charAt(i);
         }
     }
     System.out.println(s1);
    }
}

Hey I have solution for that question you can have this way.嘿,我有这个问题的解决方案,你可以通过这种方式解决。 I didn't use in-build function我没有使用内置 function

public class RemoveSpace {公共 class RemoveSpace {

 public static void main(String[] args) {
    String str="Abd is a good guy";
    
    char[] a=str.toCharArray();
    System.out.println(a);
    int size=a.length;
    for(int i=0; i<size;i++)
    {
        if(a[i]==32)
        {
            for(int j=i;j<size-1;j++)
            {
                a[j]=a[j+1];
            }
            size--;
            
        }
        
    }
    for(int i=0; i<size; i++)
    {
    System.out.print(a[i]);
    }
}}
String s = "Hello world I am here";
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
    if (c != ' ') {
        sb.append(c);
    }
}
System.out.println(sb.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM