【问题标题】:how to remove spaces in between string in java,without using library function?java - 如何在不使用库函数的情况下删除java中字符串之间的空格?
【发布时间】:2017-07-12 05:34:30
【问题描述】:

我得到的解决方法如下

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

但我不想使用库函数,即 str.replaceAll(),谁能帮我完成程序。我想要与使用库函数时相同的输出

【问题讨论】:

标签: java string logic spaces replaceall


【解决方案1】:

我从您的问题中了解到您不想使用str.replaceAll() 功能。因此,可能的替代方案如下。详情请参考Removing whitespace from strings in 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);
    }
}

【讨论】:

    【解决方案2】:

    迭代str中的每个字符。

    如果不等于 ' ',请将其附加到结果字符串 r

    但是你为什么要在不使用库函数的情况下这样做呢?

    【讨论】:

    • 这个答案如何添加其他答案尚未提供的任何内容?
    【解决方案3】:

    因为你不想保持值只是循环和打印

        String str="Hello world I am here";
    
        for (char c : str.toCharArray()) {
            if (c != ' ')
                System.out.print(c);
        }
    

    输出

    HelloworldIamhere

    当然,如果您想保留这个新字符串,那么请使用 StringBuilder 和 append char 而不是/并打印它

    【讨论】:

      【解决方案4】:
      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);
          }
      }
      

      【讨论】:

        【解决方案5】:

        嘿,我有这个问题的解决方案,你可以用这种方式。我没有使用内置功能

        公共类 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]);
            }
        }}
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        【解决方案6】:
        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());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-23
          • 1970-01-01
          • 2011-04-26
          相关资源
          最近更新 更多