String handling Java vs. C

Source: Internet
Author: User

5. String

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String name = "Tom";

String sex = "Felmale";

String from = "USA";

String info1= "Name:" +name+ "Sex:" +sex+ "from:" +from;

String Info2= "Name:" +name+ "\ n" + "Sex:" +sex+ "\ n" + "from:" +from;

System. out. println ("Info1");

System. out. println ("Info2");

}

This program outputs Info1,info2;

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String name = "Tom";

String sex = "Felmale";

String from = "USA";

String info1= "Name:" +name+ "Sex:" +sex+ "from:" +from;

String info2= "Name:" +name+ "\ n" + "Sex:" +sex+ "\ n" + "from:" +from;

System. out. println (INFO1);

System. out. println (Info2);

}

This program outputs:

Name:tom Sex:felmale From:usa

Name:tom

Sex:felmale

From:usa

(One line here)

If quoted, print at the string constant "info1" instead of treating it as a variable, without quoting the INFO1 assignment. The final print result has a blank line because println () prints a string followed by a newline step.

String greeting= "Hello china!"

String s=greeting.substring (0,5) This is intercept, get substring "Hello"

The first position of the string is 0, the position of the nth character is n-1;substring (int beginindex,int endindex), and the substring ends from Beginindex to Endindex-1. Substring (0,5) is actually a No. 0 character to a fourth character.

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String a= "Hello";

String b= "Hello";

String c=New string ("Hello");

String d=New string ("Hello");

System. out. println (a==b);

System. out. println (B==C);

System. out. println (C==d);

System. out. println (a.equals (b));

System. out. println (B.equals (c));

System. out. println (C.equals (d));

}

}

Results:

True

False

False

True

True

True

Determine two strings equal, which is the method to use equals: s1.equals (S2)

Be careful not to use "= =" to test whether two strings are equal, he is to determine whether two strings are stored in the same location.

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String a= "Hello china!";

String b=a.substring (0,6);

String c=b+ "World";

System. out. println (c);

}

}

Turn hello China into Hello World

The C language connects 2 strings:

#include <stdio.h>

#include <string.h>

Main ()

{

Char a[20]= "1234";

Char b[20]= "ABCD";

Strcat (A, b);

printf ("%s\n", a);

}

In Java:

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String a= "Hello";

String b= "World";

String C=a.concat (b);

System. out. println (c);

}

Or:

Public class World {

Public Static void Main (string[] args) {

TODO Auto-generated method stubs

String c= "he". Concat ("Llo"). Concat ("China");

System. out. println (c);

}

Int CompareTo (String other): Compares 2 strings in a dictionary order.

Boolean equalaignorecase (String otherstring): Compares this string to another string.

String handling Java vs. C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.