Sie sind auf Seite 1von 5

https://www.websparrow.

org/java/find-first-repeated-character-in-a-string-using-
java

https://netjs.blogspot.com/2019/03/find-first-repeated-character-in-string-
java.html
https://www.java-
programs.thiyagaraaj.com/first_repeated_character_java_example_program.html

https://javaconceptoftheday.com/first-repeated-and-non-repeated-character-in-a-
string/ imp

https://www.ritambhara.in/print-the-first-repeating-character-in-string/ c lang

How To Remove White Spaces From String In Java Using Built-In Methods?
In the first method, we use replaceAll() method of String class to remove all white
spaces (including tab also) from a string. This is one of the easiest method to
remove spaces from string in java. replaceAll() method takes two parameters. One is
the string to be replaced and another one is the string to be replaced with. We
pass the string �\\s+� to be replaced with an empty string ��. This method removes
spaces at the end, spaces at the beginning and spaces between the words.

import java.util.Scanner;

public class RemoveWhiteSpaces


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter input string to be cleaned from white


spaces...!");

String inputString = sc.nextLine();

String stringWithoutSpaces = inputString.replaceAll("\\s+", "");

System.out.println("Input String : "+inputString);

System.out.println("Input String Without Spaces : "+stringWithoutSpaces);

sc.close();
}
}
Output :

Enter input string to be cleaned from white spaces�!


OneSpace TwoSpaces ThreeSpaces FourSpaces Tab End
Input String : OneSpace TwoSpaces ThreeSpaces FourSpaces Tab End
Input String Without Spaces : OneSpaceTwoSpacesThreeSpacesFourSpacesTabEnd
Notes :

�\\s+� Vs �\\s� : Both of these strings, when passed to replaceAll() method,


produce the same result with almost same performance. But, when the number of
consecutive spaces increases, �\\s+� is slightly faster than �\\s�. Because, it
replaces set of consecutive multiple spaces
with the replacement string at a time rather than replacing one by one.
trim() method trims the given string i.e it removes the white spaces at the
beginning and at the end of a string, not between the words.

How To Remove White Spaces From String Without Using Built-In Methods?
This method is only for interview purpose. This method is not recommended while
developing the real time applications.

In this method, first we convert the given input string to char array and then we
traverse this array to find white spaces. We concatenate the characters which are
not the white spaces to String object.

import java.util.Scanner;

public class RemoveWhiteSpaces


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println(�Enter input string to be cleaned from white spaces�!�);

String inputString = sc.nextLine();

char[] charArray = inputString.toCharArray();

String stringWithoutSpaces = ��;

for (int i = 0; i < charArray.length; i++)


{
if ( (charArray[i] != ' ') && (charArray[i] != '\t') )
{
stringWithoutSpaces = stringWithoutSpaces + charArray[i];
}
}
System.out.println("Input String : "+inputString);
System.out.println("Input String Without Spaces : "+stringWithoutSpaces);
sc.close();
}
}
Output :

Enter input string to be cleaned from white spaces�!


OneSpace TwoSpaces ThreeSpaces FourSpaces Tab End
Input String : OneSpace TwoSpaces ThreeSpaces FourSpaces Tab End
Input String Without Spaces : OneSpaceTwoSpacesThreeSpacesFourSpacesTabEnd

2nd method could be improved by using String.charAt() and Character.isWhitespace()

import java.util.Scanner;
public class white {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println(�Enter the String�);
String input = scan.nextLine();
String result = input.replace(� �, ��);
System.out.println(result);
}
}

package StringLogicalPrograms;

public class RemoveWhiteSpace {


public static void RemoveAllSpaces(String str){
String[] array= str.split(� �);
String newString=��;

for(String s:array){
newString +=s;
}
System.out.println(newString);
}

public static void main(String[] args) {


String str=� deep is the ocean �;
RemoveAllSpaces(str);
}
}

public static void removeAllWhiteSpace(String str){


String[] strArr = str.split(� �);
StringBuilder sb = new StringBuilder();
for(String str1: strArr){
sb.append(str1);

}
System.out.println(�String without Space =�+sb);
}

public static void main(String�args){


String str=�I Love Java�;
String s[]=str.split(� �);
String newStr=��;
for(int i=0;i<s.length;i++){
newStr=newStr+s[i];
}
System.out.println(newStr);
}

public static void main(String[] args) {


String occ = �Java J2E Java JSP J2EE Java�;
HashMap hm = new HashMap();
String rspace=��;
String [] ss =occ.split(� �);
for( String s: ss){
rspace=rspace.concat(s);
}
System.out.println(rspace);
there is some performance difference doing this in this way?

public static void main(String[] args){


String str = � aver que pasa womtw �;
String res =��;
for (int i = 0; i < str.length(); i++)
if( !Character.isWhitespace(str.charAt(i)))
res+=str.charAt(i);
System.out.println("["+res+"]");
}

Das könnte Ihnen auch gefallen