java


how to substring characters from string in java


I have below line -
lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config
And i want to extract the Xmx value i.e. 2048 in this case (It can also be 512 some times). Not sure how i should do this using java substring or any other way , Please help! .
Thanks
The comment given by #Spiderman might be a quick one line solution. But in general the way to approach this type of regex problem in Java is to use a Pattern with a Matcher. The following regex can be used here:
.*\s-Xmx(\d+)(\w+)\s.*
Full code:
String line = "lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config";
String pattern = ".*\\s-Xmx(\\d+)(\\w+)\\s.*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(1));
System.out.println("Found unit: " + m.group(2));
}
Output:
Found value: 2048
Found unit: m
Demo here:
Rextester
Also this can be done without using Regex. Simply use split() method as below:
public class HelloWorld {
public static void main(String[] args) {
String line = "lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config";
String number = line.split("Xmx")[1].split("m")[0];
System.out.println(number);
}
}

Related Links

Synchronous multithreading in Java (Apache HTTPClient)
Java - Is it safe to suppress unchecked cast warning with WatchEvent?
EJB Stateless session bean is null
Open URL with Java, then close the browser [duplicate]
Java, For Loop in Class
How to exclude specific columns when generating entities from existing DB?
Importing Apache Wink using Eclipse
Hibernate stores Calendar as int when using SQLite
What is the meaning of the following statment from play! documentation?
log4j:ERROR Could not parse url [file://…/mda/log4j.xml]
Seralize a string and save it into a MySQL database in Java
VideoCapture is not working
Play production mode and writing public folder
Reverse each row of a 2D array [closed]
ConcurrentModificationException when I acces to list of list without modifying
POSTing data with many to one relationship using Thymeleaf

Categories

HOME
ibm-bluemix
testing
winforms
hive
deployment
activiti
dictionary
elm
icloud
angular-ui-bootstrap
indesign
gnupg
user-input
correlation
resize
percona
try-catch
tostring
transformation
aurigma
oracle-coherence
shared-hosting
clickonce
extjs5
p-value
info.plist
applozic
strncpy
libuv
srcset
instant-messaging
http-status-code-503
siesta-swift
google-sites-2016
repo
uiswipegesturerecognizer
wixsharp
hockeyapp
gesture
twitch
ansible-playbook
tasker
sfdc
janrain
arena-simulation
midl
slick-3.0
btrace
mu
drupal-6
carrot
forever
windows-iot-core-10
color-profile
background-service
google-cdn
yt-project
abcpdf9
jquery-filter
instant
removechild
infix-notation
android-textview
sts-springsourcetoolsuite
azure-virtual-network
transmitfile
python-stackless
packagist
livequery
oauth2client
revolution-r
energy
jfugue
wso2cloud
iis-arr
skos
cdt
phalanger
cctv
document-classification
elliptic-curve
jubula
jquery-layout
system.net.webexception
cloud-connect
typo3-neos
prettify
farseer
easy-install
cos
comaddin
hamiltonian-cycle
punbb
genshi
coredump
django-tagging
actionview
gdlib
mysql-error-1005
site.master
fluent-interface
dbal
mirah
castle-monorail
ncqrs
ctp4
thread-local-storage
ugc
.net-1.0

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile