Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Breaking up string help java
New Posts  All Forums:Forum Nav:

Breaking up string help java

post #1 of 6
Thread Starter 
We are given a line of text (string). The the front part of the line has numbers in it (no spaces). It is separated from the rest of the string by a space. There can be other spaces in the rest of the string. We want to pull just the first block of numbers. There can be numbers in the rest of the string as well.

For example, if the given string was "1230978 l;asdjf123098 239082 ajksdlqpo", we would want to return "1230978", and that's it. I've tried a bunch of different ways to do this, but I can't get it to work. Any help would be appreciated.
post #2 of 6
Code:
    public String getStringBeforeSpace(String input){
        int indexOfSpace = input.indexOf(" ");
        return input.substring(0, indexOfSpace);
    }
Here ya go man.
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
2600k @ 4.6Ghz Biostar TP67XE Powercolor 6870x2 Sapphire 6870 
RAMHard DriveHard DriveHard Drive
4 x 4GB Patriot Viper Xtreme @ 1600 CL8 Intel X-25M 80Gb Hitachi Deskstar 1Tb Seagate 1Tb 
CoolingCoolingMonitorPower
Cogage Arrow 2x Thermalright TY-140 Yamakasi 2703 2560x1440 Seasonic S12D 850w 
Case
Aerocool VS-9 Ghetto Mod 
  hide details  
Reply
post #3 of 6
Thread Starter 
edit: never mind
Edited by Hickeydog - 3/26/11 at 6:34pm
post #4 of 6
Thread Starter 
Quote:
Originally Posted by foilfence View Post
Code:
    public String getStringBeforeSpace(String input){
        int indexOfSpace = input.indexOf(" ");
        return input.substring(0, indexOfSpace);
    }
Here ya go man.
And that is a totally better way of doing it. Thank you very much.
post #5 of 6
Do you need to do any error checking in case the string doesn't contain spaces?
post #6 of 6
Checking for "indexOfSpace == -1" will let you deal with the case where there is no space.
Balder
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.4ghz Gigabyte Sniper 3 (z77) EVGA 980ti SC+ ACX 2.0 16GB (2x8) Corsair Vengeance - 1600mhz 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 (512MB) 4 x WD black 1TB LG Blueray Burner Corsair H100 
OSMonitorKeyboardPower
Windows 10 x64 Pro NEC Multisync 3090wqxi (2560x1600) Corsair k90 Corsair AX1200i 
CaseMouseAudioAudio
Corsair 600t white Naos 8200 Sennheiser HD 650 Zero DAC / Onkyo 875 
  hide details  
Reply
Balder
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k @ 4.4ghz Gigabyte Sniper 3 (z77) EVGA 980ti SC+ ACX 2.0 16GB (2x8) Corsair Vengeance - 1600mhz 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 (512MB) 4 x WD black 1TB LG Blueray Burner Corsair H100 
OSMonitorKeyboardPower
Windows 10 x64 Pro NEC Multisync 3090wqxi (2560x1600) Corsair k90 Corsair AX1200i 
CaseMouseAudioAudio
Corsair 600t white Naos 8200 Sennheiser HD 650 Zero DAC / Onkyo 875 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Breaking up string help java