Home / ພື້ນຖານ Java / String
String ເປັນຂໍ້ມູນປະເພດອອບເຈັກ ແລະ ໃນການປະກາດຕົວແປຣນັ້ນເຮັດໄດ້ໂດຍຄາດ String ໃນພາສາ Java ນັ້ນມີຄາດ String ເພື່ອຊ່ວຍໃນການຈັດການກັບຂໍ້ມູນທີ່ເປັນຂໍ້ຄວາມທີ່ຕໍ່ເນື່ອງກັນ String ນັ້ນແຕກຕ່າງຈາກຕົວອັກສອນ ຫລື ຂໍ້ມູນແບບ char ເຊິ່ງເກັບຂໍ້ມູນໄດ້ພຽງຕົວດຽວ ນອກຈາກນີ້ String ຍັງມີເມຕອດສຳລັບຈັດການຂໍ້ມູນເພື່ອອຳນວຍສະດວກສຳລັບນັກພັດທະນາໂປຣແກຣມ ມາເບິ່ງຕົວຢ່າງການປະກາດ String ໃນພາສາ Java
// method 1
String str = "Declaring string in Java.";
// method 2
String str2 = new String("Creating with String constructor.");
// method 3
char[] chs = new char[] {'C', 'h', 'a', 'r', 'a', 'r', 'r', 'a', 'y'};
String str3 = new String(chs);
ໃນຕົວຢ່າງ ເຮົາສາມາດປະກາດຕົວແປຣ String ໄດ້ 3 ວິທີ ວິທີທຳອິດແມ່ນເປັນວິທີປົກກະຕິທີ່ເຮົາໄດ້ໃຊ້ມາກ່ອນໜ້າແລ້ວ ມັນສະດວກ ແລະໃຊ້ງານງ່າຍທີ່ສຸດໂດຍກຳນົດຄ່າຂອງ String ໃຫ້ກັບຕົວແປຣໂດຍ String literal ນັ້ນຈະຢູ່ໃນເຄື່ອງໝາຍ Double quote ວິທີທີ່ສອງເປັນການປະກາດ String ຜ່ານ Constructor ຂອງຄາດ ແລະ ວິທີສຸດທ້າຍເປັນການປະກາດ String ດ້ວຍ Char array ຜ່ານ Constructor ເຊັ່ນກັນ ເຮົາສາມາດໃຊ້ວິທີໃດກໍ່ໄດ້ ເຊິ່ງໄດ້ຜົນລັບທ໌ທີ່ບໍ່ແຕກຕ່າງກັນ
ເຮົາໄດ້ເວົ້າກ່ຽວກັບການດຳເນີນການກັບ String ແລ້ວໃນບົດຂອງຕົວດຳເນີນການ String Concatenation ນັ້ນເປັນການນຳ String ຕັ້ງແຕ່ໜຶ່ງ ຫລື ຫຼາຍອັນມາຕໍ່ກັນ ໂດຍການໃຊ້ເຄື່ອງໝາຍບວກ (+) ຈະໄດ້ String ແລະ ເນື່ອງຈາກ String ນັ້ນເປັນຄາດ ໃນການໃຊ້ເຄື່ອງໝາຍ + ກັບ String ຈຶ່ງເປັນການ Overloading operator
String firstName = "Bob";
String lastName = "Henry";
String fullName = firstName + " " + lastName;
ເຮົາໄດ້ເຮັດການຕໍ່ 3 String ເຂົ້ານຳກັນ ແລະ ນຳຄ່າທີ່ໄດ້ຈາກການຕໍ່ໃສ່ຕົວແປຣໃໝ່ fullName ຜົນລັບທີ່ໄດ້ຄືມັນຈະມີຄ່າເປັນ "Bob Henry" ທີ່ເກີດຈາກການເຊື່ອມຕໍ່ກັນຂອງ String ທັງສາມ
ມາເບິ່ງຕົວຢ່າງເພິ່ມເຕິມກ່ຽວກັບການໃຊ້ງານ String ພາສາ Java ເຊິ່ງເປັນໂປຣແກຣມຖາມຜູ້ໃຊ້ໃຫ້ປ້ອນຊື່ ອາຍຸ ແລະ ກີລາ ທີ່ເຮົາມັນຂອງເຮົາ
public class Constant {
public static void main(String[] args)
{
String name;
int age;
String sport;
Scanner reader = new Scanner(System.in);
Scanner reader2 = new Scanner(System.in);
System.out.print("What's your name?: ");
name = reader.nextLine();
System.out.print("How old are you?: ");
age = reader2.nextInt();
System.out.print("What's your favorite sport?: ");
sport = reader.nextLine();
System.out.println("Hello " + name);
System.out.print("You was born in " + (2017 - age));
System.out.println(" and loves to play " + sport);
}
}
ໃນຕົວຢ່າງ ເປັນໂປຣແກຣມໃນການຖາມຂໍ້ມູນຂອງຜູ້ໃຊ້ແລະສະແດງຂໍ້ມູນເຫຼົ່ານັ້ນດ້ວຍການຕໍ່ String ໃນຕອນແຮກຂອງໂປຣແກຣມ ເຮົາໄດ້ປະກາດຕົວແປຣ name ສຳລັບເກັບຊື່ age ສຳລັບເກັບອາຢຸ ແລະ sport ສຳລັບເກັບກີລາໂປຣດ
Scanner reader = new Scanner(System.in);
Scanner reader2 = new Scanner(System.in);
ໝາຍເຫດ: ເຮົາໄດ້ປະກາດອອບເຈັກຈາກຄາດ Scanner ສຳລັບອ່ານຮັບຄ່າທາງຄີບອດ ໂດຍຕົວແປຣ reader ໃຊ້ສຳລັບຮັບຄ່າທີ່ເປັນ String ແລະ ຕົວແປຣ reader2 ຈະໃຊ້ສຳລັບຄ່າທີ່ເປັນຕົວເລກ ເຮົາຈະເປັນຕ້ອງແຍກເພື່ອໃຫ້ໂປຣແກຣມສາມາດຈັດການກັບຂໍ້ມູນໃນ Stream ໄດ້ຖືກຕ້ອງໃນຕອນຮັບຄ່າ
System.out.print("What's your name?: ");
name = reader.nextLine();
System.out.print("How old are you?: ");
age = reader2.nextInt();
System.out.print("What's your favorite sport?: ");
sport = reader.nextLine();
ສະແດງຂໍ້ຄວາມເພື່ອຖາມຜູ້ໃຊ້ມາປ້ອນຂໍ້ມູນຊື່ ອາຍຸ ແລະ ກີລາທີ່ມັກ ຕາມລຳດັບ
System.out.println("Hello " + name);
System.out.print("You was born in " + (2017 - age));
System.out.println(" and loves to play " + sport);
ນິ້ເປັນການຕໍ່ String ໃນຂັ້ນພື້ນຖານ ໃນຄຳສັ່ງທຳອິດເຮົາໄດ້ສະແດງຄຳທັກທາຍຜູ້ໃຊ້ໂດຍການຕໍ່ຄຳທັກທາຍ ແລະ ຊື່ຂອງທ່ານເຂົ້ານຳກັນ ໃນຄຳສັ່ງທີ່ສອງ ເຮົາໄດ້ຄິດໄລ່ຫາປີທີ່ເກີດໂດຍການນຳອາຍຸໄປລົບກັບ 2016 ໃນຄຳສັ່ງນີ້ເປັນການຕໍ່ String ກັບຂໍ້ມູນປະເພດ Integer ເຊິ່ງຈະເຮັດໃຫ້ Literal ຂອງ Integer ນັ້ນຖືກແປງເປັນ String ເຊິ່ງຫຼັກການນີ້ສາມາດເກີດຂຶ້ນໄດ້ເມື່ອເຮົານຳ String ໄປຕໍ່ກັບປະເພດຂໍ້ມູນຊະນິດອື່ນ ເຊິ່ງຈະໄດ້ເປັນ Literal ຂອງຂໍ້ມູນຊະນິດນັ້ນໃນຮູບແບບຂອງ String ແລະຄຳສັ່ງສຸດທ້າຍເປັນການສະແດງເຖິງກີລາທີເຮົາມັກ
What's your name?: John
How old are you?: 28
What's your favorite sport?: Hockey
Hello John
You was born in 1988 and loves to play Hockey
ເພາະວ່າ String ນັ້ນເປັນຄາດ ດັ່ງນັ້ນມັນຈຶ່ງມີເມຕອດຕ່າງໆ ທີ່ໃຫ້ເຮົາສາມາດໃຊ້ງານໄດ້ ເມຕອດກໍ່ຄືຟັງກ໌ຊັ່ນທີ່ໃຊ້ໃນການຈັດການຂໍ້ມູນ ເພື່ອໃຫ້ໄດ້ຜົນລັບອອກມາຕາມຫນ້າທີ່ຂອງມັນ ເຮົາຈະໄດ້ຮຽນກ່ຽວກັບເມຕອດໃນບົດຕໍ່ໄປ ມາເບິ່ງຕົວຢ່າງການໃຊ້ເມຕອດທີ່ສຳຄັນຂອງ String
public class StringMethod {
public static void main(String[] args) {
String name = "Kittisay";
System.out.println("Length is " + name.length());
System.out.println("Char at index 6 is " + name.charAt(6));
if (name.contains("Code")) {
System.out.println("'Code' is in '" + name + "'");
} else {
System.out.println("'Code is not in '" + name + "'");
}
System.out.println("Index of 'cus' is at " + name.indexOf("cus"));
name = name.concat(" tutorials");
System.out.println(name);
name = name.replace(" tutorials", "'s Programming");
System.out.println(name);
name = name.toUpperCase();
System.out.println(name);
name = "";
if (name.isEmpty()) {
System.out.println("name is empty.");
}
}
}
ຂ້າງເທິງເປັນໂຄ້ດຂອງການໃຊ້ເມຕອດໃນຄາດ String ເຮົາໄດ້ຫາຄວາມຢາວຂອງມັນ ເບິ່ງຕົວອັກສອນທີ່ຕຳແໜ່ງ index 6 ກວດສອບວ່າມີ String ຍ່ອຍຢູ່ຂ້າງໃນ ຫລື ບໍ່ ແລະ ກວດສອບ index ຂອງ cus ທີ່ຢູ່ພາຍໃນ String ຕໍ່ String ໃໝ່ tutorials ເຂົ້າມາ ແລ້ວແທນທີ່ດ້ວຍ Programming ຫຼັງຈາກນັ້ນແປງໃຫ້ເປັນຕົວພິມໃຫຍ່ທັງໝົດ ແລະ ກຳນົດໃຫ້ String ວ່າງ ແລະ ກວດສອບໃນຕອນທ້າຍ
Length is 10
Char at index 6 is C
'Code' is in 'Kittisay'
Index of 'cus' is at 3
Kittisay tutorials
Kittisay's Programming
KITTISAY'S PROGRAMMING
name is empty.
Type
char
int
String
boolean
boolean
boolean
static String
byte[]
int
boolean
int
boolean
String
String[]
boolean
String
char[]
String
String
String
String
static String
static String
static String
static String
static String
static String
static String
Method
charAt(int index)
compareTo(String anotherString)
concat(String str)
contains(CharSequences)
endsWith(String suffix)
equals(Object anObject)
format(String format, Object... args)
getBytes()
indexOf(Stringstr)
isEmpty()
length()
matches(String regex)
replace(CharSequence target, CharSequence replacement)
split(String regex)
startsWith(String prefix)
substring(int beginIndex, int endIndex)
toCharArray()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf(boolean b)
valueOf(char c)
valueOf(double d)
valueOf(float f)
valueOf(int i)
valueOf(long l)
valueOf(Object obj)
Description
Returns the char value at index.
Compares two strings.
StriConcatenates the string to the end.ng
Returns true if this string contains the specified sequence.
Tests if this string ends with suffix.
Compares this string to the specified object.
Returns a formatted string.
Encodes this String into a sequence of bytes.
Returns the index within this string of the first occurrence substring.
Returns true if, and only if, length() is 0.
Returns the length of this string.
Tells whether or not this string matches regular expression.
Replaces each substring.
Splits this string around matches.
Tests if this string starts with the prefix.
Returns a new string that is a substring
Converts this string to a new character array.
Converts all of the characters in this String to lower case.
This object is itself returned.
Converts all of the characters in this String to upper case.
Returns a copy of the string.
Returns the string representation of the boolean argument.
Returns the string representation of the char argument.
Returns the string representation of the double argument.
Returns the string representation of the float argument.
Returns the string representation of the int argument.
Returns the string representation of the long argument.
Returns the string representation of the Object argument.
ໃນການຂຽນໂປຣແກຣມ ມີຂໍ້ມູນປະເພດຕ່າງໆ ທີ່ເຮົາຕ້ອງຈັດການ ຂໍ້ມູນແຕ່ລະແບບນັ້ນສຳລັບພາສາ Java ຕ້ອງເກັບໄວ້ໃນຕົວແປຣໃນແຕ່ລະປະເພດຂອງມັນເທົ່ານັ້ນ ເຊັ່ນ ຕົວເລກຈຳນວນເຕັມ ຈະເກັບໃນຕົວແປຣປະເພດ short int ຫລື long ຕົວເລກທົດນິຍົມເກັບໃນ float ຫລື double ແລະ ຂໍ້ຄວາມຫລື string ເກັບໃນຕົວແປຣປະເພດ String ໂດຍການແປງຂໍ້ມູນໃນພາສາ Java ມີດັ່ງນີ້
newType variableName = (newType) valueToCovert;
newType ປະເພດຂໍ້ມູນທີ່ຕ້ອງການແປລງໄປນັ້ນ ຈະຕ້ອງເປັນປະເພດຂໍ້ມູນແບບ primitive date type ເທົ່ານັ້ນ ຄື char short int float double ຍົກເວັ້ນ boolean valueToCovert ເປັນຂໍ້ມູນປະເພດເດີມທີ່ຕ້ອງການແປລງໄປຍັງປະເພດໃໝ່ ໂດຍສາມາດເປັນໄດ້ທັງຕົວແປຣ ແລະ literals
public class TypeConversions {
public static void main (String[] args) {
float f = 5.0f;
long newF = (long) f;
System.out.println("Cast float to long = " + newF);
System.out.println("Cast double to int = " + (int) 123.345);
System.out.println("Cast int to float = " + (float) 100);
float n1 = 10.12f;
System.out.println("n1 is float = " + n1);
System.out.println("Cast double to float = " + (double) n1);
double n2 = 10.12;
System.out.println("n2 is double = " + n1);
System.out.println("Cast float to double = " + (float) n2);
}
}
ໃນຕົວຢ່າງດ້ານເທິງເປັນການແປລງຂໍ້ມູນປະເພດຕ່າງໆ (ເຮົາມັກຈະໃຊ້ຄຳວ່າ Casting ແທນສຳລັບວິທີນີ້) ເຮົາໄດ້ແປລງຕົວເລກອີກປະເພດໜຶ່ງໄປເປັນອີກປະເພດໜຶ່ງ ໂດຍວິທີການ Type Casting
Cast float to long = 5
Cast double to int = 123
Cast int to float = 100.0
n1 is float = 10.12
Cast double to float = 10.119999885559082
n2 is double = 10.12
Cast float to double = 10.12
ໝາຍເຫດ: ການແປລງຂໍ້ມູນທີ່ມີຂະໜາດໃຫຍ່ກວ່າໄປເປັນຂໍ້ມູນທີ່ນ້ອຍກວ່າ ອາດຈະເຮັດໃຫ້ເກີດຄວາມຜິດພາດເຊິ່ງເຮົາເອີ້ນວ່າ data loss ຫລື precision loss ເຊັ່ນການແປລງຈາກ long ໄປເປັນ int ຫລື ຈາກ double ໄປເປັນ float ເປັນຕົ້ນ
ໃນພາສາ Java ຄາດຂອງ primitive data type ນັ້ນຈະມີທັງໝົດ 8 ຄາດດັ່ງນີ້ ຄືທີ່ເຮົາໄດ້ເວົ້າໄປໃນບົດຂອງຕົວແປຣແລະ ປະເພດຂໍ້ມູນ
Class Name
Byte
Short
Integer
Long
Float
Double
Character
Boolean
Type
byte
short
int
long
float
double
char
boolean
ໂດຍໃນແຕ່ລະຄາດຈະມີເມຕອດໃນການເຮັດວຽກງານທີ່ແຕກຕ່າງກັນອອກໄປ ໂດຍສ່ວນຫຼາຍແລ້ວເມຕອດເຫຼົ່ານີ້ຈະໃຊ້ກັບຂໍ້ມູນທີເປັນ string ເປັນສ່ວນໃຫຍ່ ຂ້າງລຸ່ມນີ້ເປັນຕົວຢ່າງ ໃນການໃຊ້ເມຕອດໃນຄາດຕ່າງໆ
public class PrimitiveTypeClass {
public static void main (String[] args) {
// Convert from string
System.out.println("Covert string to int = " + Integer.parseInt("15"));
System.out.println("Covert string to short = " + Short.parseShort("15"));
System.out.println("Covert string to long = " + Long.parseLong("15"));
System.out.println("Covert string to float = " + Float.parseFloat("11.54f"));
System.out.println("Covert string to double = " + Double.parseDouble("11.54"));
// Convert to string
System.out.println("Covert int to string = " + Integer.toString(10));
System.out.println("Covert int to base 16 = " + Integer.toHexString(10));
System.out.println("Covert int to base 8 = " + Integer.toOctalString(10));
System.out.println("Covert int to base 2 = " + Integer.toBinaryString(10));
}
}
ເຮົາໄດ້ເຮັດການແປລງຂໍ້ມູນໂດຍໃຊ້ເມຕອດຈາກຄາດຕ່າງໆ ເຮົາຈະເຫັນວ່າບາງຢ່າງໃຊ້ເມຕອດທີ່ແຕກຕ່າງກັນ ເຊັ່ນ ແປລງຂໍ້ມູນແບບ String ໄປເປັນ Integer ຈະໃຊ້ເມຕອດ Integer.parseInt ໃນຂະນະທີ່ແປລງໄປ Short ຈະໃຊ້ Short.parseShort ແລະ ນອກຈາກນີ້ຢັງແປລງໄປເປັນເລກຖານອື່ນໃນຮູບແບບ String ໄດ້ ເຊັ່ນ ຖານ 16 ຖານ 8 ແລະຖານ 2 ໄດ້ອີກດ້ວຍ
ໝາຍເຫດ: ໃນການໃສ່ຄ່າຂອງ String ເຂົ້າໄປຢັງເມຕອດ parseInt ຫລື parse... ຮູບແບບຂອງຂໍ້ມູນຕ້ອງຖືກຕ້ອງຕາມ Literals ຂອງຂໍ້ມູນແບບຕົວເລກທຸກເທື່ອ
Covert string to int = 15
Covert string to short = 15
Covert string to long = 15
Covert string to float = 11.54
Covert string to double = 11.54
Covert int to string = 10
Covert int to base 16 = a
Covert int to base 8 = 12
Covert int to base 2 = 1010
ຈາກຕົວຢ່າງດ້ານເທິງເປັນພຽງແຕ່ໃຊ້ເມຕອດສ່ວນຫນຶ່ງເທົ່ານັ້ນ ເຊິ່ງຍັງມີເມຕອດອີກຫລວງຫຼາຍທີ່ເຮົາສາມາດໄປລອງໃຊ້ໄດ້ ເຮົາແນະນຳໃຫ້ເຮົາສຶກສາຈາກ Auto Suggestion ຈາກ IDE ເພຣາະມັນມີຄຳອະທິບາຍໃຫ້ເຮົາຢ່າງລະອຽດ
ຈາກຮູບເປັນການໃຊ້ປະໂຫຍດຈາກ IDE ເຮົາເອີ້ນວ່າ Code Completion (ຂຶ້ນກັບໂປຣແກຣມ) ແນ່ນອນວ່າເຮົາສາມາດນຳເຄັດລັບນີ້ໄປໃຊ້ໄດ້ກັບທັງອອບເຈັກທີ່ເຮົາສ້າງຂຶ້ນເອງໄດ້
ຄາດ String ໃຊ້ສຳລັບແປລງຂໍ້ມູນປະເພດຕ່າງໆ ເປັນ String ນິ້ເປັນຕົວຢ່າງການແປລງຂໍ້ມູນເປັນ String ເຊັ່ນ ແປລງຕົວເລກໃຫ້ເປັນ String ຫລື ມີຄາດຈັດການກ່ຽວກັບ String ເຊິ່ງຢູ່ໃນບົດຂອງ String
public class StringTypeClass {
public static void main (String[] args) {
System.out.println("Covert boolean to string " + String.valueOf(true));
System.out.println("Covert int to string " + String.valueOf(12));
System.out.println("Covert float to string " + String.valueOf(50.4f));
System.out.println("Covert double to string " + String.valueOf(98.321));
}
}
ຈາກໂຄດນັ້ນເປັນການໃຊ້ເມຕອດ String.valueOf ເຊິ່ງມັນເປັນເມຕອດແບບ Overloading Method ທີ່ສາມາດຮອງຮັບຂໍ້ມູນໄດ້ທຸກປະເພດທີ່ເປັນອາກຽວເມັ້ນ
Covert boolean to string true
Covert int to string 12
Covert float to string 50.4
Covert double to string 98.321
ໃນບົດນີ້ ເຮົາໄດ້ຮຽນຮູ້ກ່ຽວກັບການແປລງປະເພດຂໍ້ມູນໃນພາສາ Java ເພື່ອທີ່ຈະໃຊ້ຈັດການກັບຂໍ້ມູນທີ່ຕ່າງປະເພດກັນ ໂດຍສະເພາະການແປລງຈາກຕົວເລກເປັນ String ແລະ ແປລງຈາກ String ເປັນຕົວເລກ ໂດຍໃຊ້ເມຕອດຈາກຄາດຕ່າງໆ ນອກຈາກນີ້ເຮົາຢັງຮູ້ວ່າການແປລງຂໍ້ມູນທີ່ມີຂະໜາດໃຫຍ່ກວ່າໄປຢັງຂໍ້ມູນທີ່ມີຂະໜາດນ້ອຍກວ່າອາດຈະເຮັດໃຫ້ເກີດຄວາມຜິດພາດຂອງຂໍ້ມູນໄດ້