1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
jekas [21]
3 years ago
13

How do I find a space in my String and replace it? JAVA

Computers and Technology
1 answer:
Fantom [35]3 years ago
8 0

Answer:

This article shows how to use regex to remove spaces in between a String.

A string with spaces in between.

String text = "Hello World Java.";

We want to remove the spaces and display it as below:

Hello World Java.

1. Java regex remove spaces

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.

Regex explanation.

`\\s` # Matches whitespace characters.

+ # One or more

StringRemoveSpaces.java

package com.mkyong.regex.string;

public class StringRemoveSpaces {

public static void main(String[] args) {

String text = "Hello World Java.";

String result = text.replaceAll("\\s+", " ");

System.out.println(result);

}

}

Output

Terminal

Hello World Java.

You might be interested in
I will mark brainliest
Angelina_Jolie [31]

Answer:

specialized limitations

Explanation:

beacause the others is an application product

4 0
2 years ago
What are two major techniques involved in green computing
julia-pushkina [17]

Answer

sales and consulting

Explanation

Green computing the use of computers and resources  in an environmental friendly way. This  is for the purpose of reducing the use of hazardous materials and also to maximize energy efficiency during the time the product will remain in use.  And also to promote recycling of those products after their life is over. This is very beneficial to the environment because it reduces environmental impact.

4 0
3 years ago
Design and implement a class Country that stores the name of the country, its population, its area, and the population density (
melomori [17]

Answer:

See explaination

Explanation:

Make use of Python programming language.

File: Country.py

class Country:

def __init__(self, n="", p=0, a=0):

""" Constructor """

self.name = n

self.population = p

self.area = a

self.pDensity = self.population / self.area

def getCountryName(self):

""" Getter method """

return self.name

def getPopulation(self):

""" Getter Method """

return self.population

def getArea(self):

""" Getter Method """

return self.area

def getPDensity(self):

""" Getter Method """

return self.pDensity

File: CountryInfo.py

from Country import *

# Opening file for reading

with open("d:\\Python\\data.txt", "r") as fp:

# List that hold countries

countryInfo = []

# Processing file line by line

for line in fp:

# Stripping spaces and new lines

line = line.strip()

# Splitting and creating object

cols = line.split('\t')

cols[1] = cols[1].replace(',', '')

cols[2] = cols[2].replace(',', '')

c = Country(cols[0], int(cols[1]), float(cols[2]))

countryInfo.append(c)

# Processing each countryInfo

largestPop = 0

largestArea = 0

largestPDensity = 0

# Iterating over list

for i in range(len(countryInfo)):

# Checking population

if countryInfo[i].getPopulation() > countryInfo[largestPop].getPopulation():

largestPop = i;

# Checking area

if countryInfo[i].getArea() > countryInfo[largestPop].getArea():

largestArea = i;

# Checking population density

if countryInfo[i].getPDensity() > countryInfo[largestPop].getPDensity():

largestPDensity = i;

print("\nLargest population: " + countryInfo[largestPop].getCountryName())

print("\nLargest area: " + countryInfo[largestArea].getCountryName())

print("\nLargest population density: " + countryInfo[largestPDensity].getCountryName() + "\n")

Please go to attachment for sample program run

4 0
3 years ago
What is the meaning of the phrase the video has gone viral
Kamila [148]

It means it's being spread really quickly all over the world.

3 0
3 years ago
Read 2 more answers
If you wanted to buy a house that cost $150,000 and you knew you needed a down payment of five percent, how much would you need
andrew11 [14]

Answer:

7,500 is the answer

Explanation:

7,500 is 5% of 150,000

8 0
2 years ago
Other questions:
  • The PATH environment variable.
    5·1 answer
  • Which technology was used in fourth generation of computer​
    13·2 answers
  • True or False. When used with the cout object, the endl stream manipulator advances the cursor to the next line on the computer
    6·1 answer
  • If an author is creating a reference list and wants the second and succeeding lines indented for a reference, they should select
    13·2 answers
  • A string with delimiters can be parsed into separate fields using the _____ class.
    8·1 answer
  • My pc suddenly freezes and i can't open any apps or task manager. Apps do not open at all and if i restart from the start it get
    9·1 answer
  • How do you give brianliest
    13·1 answer
  • is a security design principle to direct the selection of control layers for an organization's computing enclave to ensure its r
    6·1 answer
  • What is the correct order for writing the 3 dimensions for a 3D object? Here are the 3 dimensions:
    15·1 answer
  • Which element of the MakeCode Arcade interface represents an individual
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!