This question already has answers here:
How to get a substring of text?
(5 answers)
Closed 8 years ago.
I made a program in java like this:
class substr{
public static void main (String[]args){
int n=1;
String s1="";
String s2=" ";
while (n<=5){
System.out.println(s2.substring(n,5)+n+s1+n);
s1=s1+" ";
s2=s2+" ";
n++;
}
}
}
I can't make use of substring in ruby, so I wanted to know if there is some way. I read about regex, but you don't use it in a program like this.
You can use array notation to substring an string. In your case, something like s2[n,5].
Related
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 3 years ago.
I have a string of "A B C" and I have this line of code to split it:
String[] reFormatted = TestString.split(" ");
System.out.println(reFormatted.toString());
and the output I get is this:
[Ljava.lang.String;#30f1c0
if I put this string array in a foreach loop like this:
for(String s : reFormatted){
System.out.println(reFormatted);
}
I get the output of:
[Ljava.lang.String;#30f1c0
[Ljava.lang.String;#30f1c0
[Ljava.lang.String;#30f1c0
what is the problem here? what am I doing wrong?
I think you are printing the address of the variable reFormatted and for the for loop, you should print s not reFormatted
for(String s : reFormatted){
// System.out.println(reFormatted);
System.out.println(s);
This question already has answers here:
Immutability of Strings in Java
(26 answers)
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 4 years ago.
I was learning string concepts, so wrote a code,expected a different output but got something very unexpected.
class stringmute
{
public static void main(String[] args)
{
String s1="Hello "; //string one.
System.out.println("Str1:"+s1);
String s2= s1+"world"; //New String.
System.out.println("Str2="+s2);
s1=s1+"World!!"; //This should produce only Hello right?
System.out.println("Str1 modified:"+s1);
}
}
when I execute the above code i get the output as:
Str1:Hello
Str2=Hello world
Str1 modified:Hello World!!
if i've done something wrong please let me know.
Since strings are immutable, which implies we should get the output of the "Str1 Modified" as "HELLO" instead of "HELLO WORLD!!".
When you assign s1 as :
s1=s1+"World!!";
New String created in jvm string pool and assigned to s1.
So it's value became "Hello World!!"
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
First of all,I am newbie in Java and I came across this issue several times. I am trying to return the number of occurrences of a char in a string and I implemented a method called countcharacters which looks like this:
public static int[] countcharacters(String s)
{
String alphabet="abcdefghijklmnopqrstuvwxyz";
int[] sircounter=new int[alphabet.length()];
for(int i=0;i<sircounter.length;i++)
{
sircounter[i]=0;
}
for(int j=0;j<alphabet.length();j++)
{
for(int i=0;i<s.length();i++)
{
if(alphabet.charAt(j)==(s.charAt(i)))
{
sircounter[j]++;
}
}
}
return sircounter;
}
In other words, each time a character from alphabet is found in my string ,the argument of the method, the value zero from each position from sircounter is incremented with the number of occurrence.
I wanted to print the array position by position but it didn't work and if
I do something like this in the main method:
String text="hannah";
System.out.println(Class.countcharacters(text));
I get a hex code: [I#659e0bfd which I don't understand.
Could you help me?
Many thanks and have a nice day!
This is happening because array is treated as an object in java.And when you try to print an object in java you get its hash code(as long as that object hasn't have toString() defined!)
Try this rather-
String text="hannah";
int counter[]=Class.countcharacters(text);
for(int i=0;i<counter.length;i++){
System.out.print(counter[i]+" ");
}
This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 7 years ago.
Maybe I am using replaceAll incorrectly, but I am unable to find why it acts this way. I want to simply remove a $ sign from a string and then output the string.
public class Example{
public static void main(String[] args){
String s = "$50";
s.replaceAll("\\D+", "");
System.out.println(s);
}
}
However, this still outputs the $ symbol with the string. Does anyone know why this is happening?
You need to assign the return value of replaceAll to a variable:
s = s.replaceAll("\\D+", "");
because a String object is immutable.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
String length without using length() method in java
I need to get the length of a string (no of characters present) in Java using a for loop and without using any methods like length().
import java.io.*;
import java.util.*;
public class reversestring
{
public static void main(String arg[])throws IOException
{
String s;
int i=0,j=0,k=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter ur string : ");
s=in.readLine();
char ar[]=s.toCharArray();
System.out.println("Length of the string is : ");
for(j=ar[i];j!='\0';i++)
{
k++;
}
System.out.println(+k);
}
}
I wrote this program, but I am not getting the answer. What is wrong with it?
This has been asked before. Here's my favorite answer:
str.lastIndexOf("")
(which probably even runs in constant time, as opposed to the other answers.)
The following .length is not a method.
int length = s.toCharArray().length
Java isn't C, thus you can't treat Java strings as C strings and you can't expect C methods to work in Java. In particular, Java strings aren't null-terminated.
The 'correct' way would be to use length (either a string method, or an array property), but, since you don't want, you could employ 'for each' loop.
for (char c in charArray) {
++count;
}
It feels not good, though.
a functional guy would answer
len(str)
return str.isEmpty() ? 0 : 1+len(str.substring(1));
int counter;
String s = "something";
try{
for(counter=0; s.charAt(counter); counter++);
}catch(Exception e){
//ArrayIndexOutOfBoundsException
System.out.println("Length: " + counter);
}
I believe Java's String.toCharArray() returns an array that is not null-terminated, so looking for a null character would not work.
I can't believe all the other answers are using the String API, although the title of this question indicates that's not allowed :-P
I came up with this:
System.out.println(new StringReader("stackoverflow").skip(Long.MAX_VALUE));