-1

I have two strings


 1. "151.909 KB"
 2. "151.0"

In both cases I want to cut everything that comes after the period. The expected result is the following:


 1. "151"
 2. "151"
3
  • Don't cut it. substring it ;) Commented Aug 12, 2015 at 9:11
  • and If I'll upload over 999 kb, I'll have in result 1.1 Mb, so what then? Commented Aug 12, 2015 at 9:12
  • Please, don't just ask for something. Provide what you've tried so far, and show us you've actually had a go at trying to achieve what you want. Commented Aug 12, 2015 at 9:12

4 Answers 4

7
s.substring(0, s.indexOf("."));
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, it works :) Will accept in 10 min, can't accept ur answer right now
3
String str = "150.23121KB";
String[] requiredString = str.split("\\.");
System.out.println(requiredString[0]);

2 Comments

split takes a regex.... you should quote the dot.. Your current code will split according to everything, resulting with an array of each char.
Right..forgot that :)
2

For this use String.indexOf

Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned. For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:

Combined with with String.substring

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Result

s.substring(0, s.indexOf("."));

Comments

1
result = result.split("\\.")[0];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.