-3

I want to keep all the characters before a space and remove the rest of the string.

Example:

String str = "Davenport FL 33897 US";

I want to print only "Davenport" and remove the rest of the string. Also the string is dynamic every time.

JeffC
  • 22,180
  • 5
  • 32
  • 55
surbhi gupta
  • 61
  • 1
  • 2
  • _every time is dynamic_ do you men the _String_ value i.e. **Davenport FL 33897 US** is dynamic? – undetected Selenium Feb 26 '19 at 07:03
  • Which characters do you want to be considered as special characters? – escapeVelocity Feb 26 '19 at 07:15
  • @AdiBnaya Any reason to add _JavaScript_ within the question title? OP seems to be looking for a _Java_ solution and even your answer is based on _Java_ but not _JavaScript_. – undetected Selenium Feb 26 '19 at 09:08
  • @DebanjanB Sorry, I was looking on a lot of JavaScript code lately and automatically thought about JS – Adi Bnaya Feb 26 '19 at 12:50
  • @AdiBnaya I presumed the same. Thanks to YvetteColomb for rescuing us from the mess. NicoHaase will be having a clear understanding of the situation now. – undetected Selenium Feb 26 '19 at 13:00
  • Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – JeffC Feb 26 '19 at 17:32
  • If your city, state zip string is dynamic you will want to be careful about splitting by space. Many city names in the US (and elsewhere) have spaces in them, e.g. "New York City". You might split by space and then ignore the last three substring (city, zip, country) and put the rest back together. For example, "New York City NY 10001 US" you would split by space leaving "New", "York", "City", "NY", "10001", "US". Ignoring the last three gives you "New", "York", "City" which you put back together to "New York City". – JeffC Feb 26 '19 at 17:38

4 Answers4

1

You need to use substr and indexOf

str.substr(0,str.indexOf(' ')); // "Davenport"

substr(0,X) will give you the sub string of your string starting at index 0 and ending at index X, Because you want to get the first word until the first space you replace X with:

str.indexOf(' ')

which returns the index of the first space in your string

Adi Bnaya
  • 473
  • 4
  • 14
0

We can use regular expression to detect space and special character. You can utilize below code to do the same.

String str="Davenport FL 33897 US";
String[] terms = str.split("[\\s@&.?$+-]+");
System.out.println(terms[0]);

It's working for me.

Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • 1
    Thanks. I have mentioned how we can achieve the solution. – Muzzamil Feb 26 '19 at 07:40
  • Since when does Javascript use `System.out.println`? – Nico Haase Feb 26 '19 at 08:43
  • @Muzzamil Why do you really need multiple delimiters? In this particular usecase I think `.split(" ");` will do the job – undetected Selenium Feb 26 '19 at 09:19
  • 1
    @NicoHaase There is a huge mess up with this post. AdiBnaya 's edit have completely vandalized this post. OP never opted for a JavaScript based solution. Even though AdiBnaya 's answer is based on _Java_ but not _JavaScript_. The _JavaScript_ tag is unnecessary here. – undetected Selenium Feb 26 '19 at 09:22
  • 1
    Thanks for that clarification @DebanjanB - but just to be nitpicking, the JS tag has been in the original post already – Nico Haase Feb 26 '19 at 09:29
  • @NicoHaase Yes, you saw it right. The new contributors (though OP is no more a new contributor) at times tends to messup _Java_ and _JavaScript_ but again _Selenium_'s _Java_ binding do executes _JavaScript_. hence these kinda messup happens. – undetected Selenium Feb 26 '19 at 09:40
  • @DebanjanB .split(" "); will work for space only but we need to take care of special characters also. – Muzzamil Feb 26 '19 at 11:19
  • Theoretically you are correct. But this _usecase_ doesn't need anything other delimiter except `split(" ");`. Adding extra delimiter would increase processing time (possibly degrade performance ??). – undetected Selenium Feb 26 '19 at 11:22
0

You can use the split function to split the string using ' ' as your delimeter.

const myString = "Davenport FL 33897 US"

const splitString = myString.split(' ')

split() returns an array of strings that separated using the delimeter. you can just get the index of the first string by.

console.log(splitString[0]) 

another way would be just to put the index after the split function so that you get only what you want and not the full array.

const firstWord = myString.split(' ')[0]
AN DI LE
  • 96
  • 3
-2
String[] newOutput = str.split(" ");
String firstPart = newOutput[0];
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352