Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

splitting a string  RSS feed

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Name/username Host Dyn Nat ACL Port Status
5102/5102 (Unspecified) D 0 Unmonitored


From the above i have to display the Name/username alone.I have tried the below code

it gives the following output


Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host
Name/username Host Dyn
Name/username Host Dyn Nat
Name/username Host Dyn Nat ACL
Name/username Host Dyn Nat ACL Port
Name/username Host Dyn Nat ACL Port
Name/username Host Dyn Nat ACL Port
Name/username Host Dyn Nat ACL Port
Name/username Host Dyn Nat ACL Port
Name/username Host Dyn Nat ACL Port Status
Name/username Host Dyn Nat ACL Port Status
Name/username Host Dyn Nat ACL Port Status
Name/username Host Dyn Nat ACL Port Status
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102
5102/5102 (Unspecified)
5102/5102 (Unspecified)
5102/5102 (Unspecified)


I think i have to check the string length and i have to use the separator to split the strings.but i dont know how to do in code.anybody explain me in code please.

Thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mystring = "firststring\secondstring some words"

First split it using array[] = mystring.split(" ");

then only that the first i.e names[] = array[0].split("\")
names[0] = "firststring"
names[1] = "secondstring"

a working example

public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}

public void doit() {
// extra spaces
String s3 = "Real How To";
String [] temp = null;
temp = s3.split(" ");
dump(temp);
}

public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real

How
To
------------
 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mystring = "firststring\secondstring some words"

First split it using array[] = mystring.split(" ");

then only that the first i.e names[] = array[0].split("\")
names[0] = "firststring"
names[1] = "secondstring"

a working example

public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}

public void doit() {
// extra spaces
String s3 = "Real How To";
String [] temp = null;
temp = s3.split(" ");
dump(temp);
}

public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real

How
To
------------
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Scanner class.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!