2010. 7. 26. 19:19


회사 프로젝트 코드에서 공백이 포함된 문자열을 공백을 제거할 로직을 추가할 일이 있어서
아래와 같은 방식으로 수정을 하였습니다.

 
1 String str = "Vicki Yi";
2   
3 System.out.println(str + " : " + str.replaceAll(" ", ""));



허나 결과는...

Vicki Yi : Vicki Yi


정규식에서 공백은 정확하게 공백으로 인식을 못하는 것 같았습니다.
Pattern JavaDoc 에서 내용을 확인하였습니다.
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

POSIX character classes (US-ASCII only)
\p{Lower} A lower-case alphabetic character: [a-z]
\p{Upper} An upper-case alphabetic character:[A-Z]
\p{ASCII} All ASCII:[\x00-\x7F]
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]
\p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
\p{Graph} A visible character: [\p{Alnum}\p{Punct}]
\p{Print} A printable character: [\p{Graph}\x20]
\p{Blank} A space or a tab: [ \t]
\p{Cntrl} A control character: [\x00-\x1F\x7F]
\p{XDigit} A hexadecimal digit: [0-9a-fA-F]
\p{Space} A whitespace character: [ \t\n\x0B\f\r]


그래서 아래와 같이 수정하였습니다.

 
1 String str = "Vicki Yi";
2   
3 System.out.println(str + " : " + str.replaceAll("\\p{Space}", ""));



결과는~~~ 원하는 결과가 나왔습니다.

Vicki Yi : VickiYi



정규식을 안쓰면 indexOf() 와 replace 를 while 문으로 돌려서 처리를 해야 하기에 역시 정규식이 깔끔한 것 같습니다.

출저 -  http://vicki.tistory.com/583

'JAVA 6.0 > Fundamentals' 카테고리의 다른 글

BufferedWriter 파일에 데이터 삽입  (0) 2009.11.15
BufferedReader 파일 읽어오기  (0) 2009.11.15
Posted by TIMEC
2009. 11. 15. 20:07
Posted by TIMEC
2009. 11. 15. 20:03
Posted by TIMEC
2009. 11. 15. 20:00
Posted by TIMEC
2009. 11. 15. 19:58

BufferedWriter bw =new BufferedWriter(new FileWriter("c:\\log.txt"));
  
  for(int i=0;i<vc.size();i++){
   bw.write(vc.elementAt(i).getYear()+"/");
   bw.write(vc.elementAt(i).getMonth()+"/");
   bw.write(vc.elementAt(i).getDay()+"/");
   bw.write(vc.elementAt(i).getHour()+"/");
   bw.write(vc.elementAt(i).getMinute()+"/");
   bw.write(vc.elementAt(i).getTotalHour()+"/");
   bw.write(vc.elementAt(i).getTotalMinute()+"/");
  }
  bw.close();
 }

bw.close(); <-- 안해주면 파일이 들어가지 않음.

'JAVA 6.0 > Fundamentals' 카테고리의 다른 글

replaceAll로 공백 제거하기  (0) 2010.07.26
BufferedReader 파일 읽어오기  (0) 2009.11.15
Posted by TIMEC
2009. 11. 15. 19:56

BufferedReader in = new BufferedReader(new FileReader("c:\\log.txt"));
  String line=in.readLine();
  
  StringTokenizer st = new StringTokenizer(line,"/");
  
  while(st.hasMoreTokens()){
  di = new DateInfo();
  di.setYear(Integer.parseInt(st.nextToken()));
  di.setMonth(Integer.parseInt(st.nextToken()));
  di.setDay(Integer.parseInt(st.nextToken()));
  di.setHour(Integer.parseInt(st.nextToken()));
  di.setMinute(Integer.parseInt(st.nextToken()));
  di.setTotalHour(Integer.parseInt(st.nextToken()));
  di.setTotalMinute(Integer.parseInt(st.nextToken()));
  
  vc.add(di);
  }

'JAVA 6.0 > Fundamentals' 카테고리의 다른 글

replaceAll로 공백 제거하기  (0) 2010.07.26
BufferedWriter 파일에 데이터 삽입  (0) 2009.11.15
Posted by TIMEC
2009. 11. 15. 19:53

package layout;

import java.awt.*;
import java.util.*;

public class PositionLayout implements LayoutManager {
    Vector  positions = new Vector(10);
    Vector  components = new Vector(10);
    private  boolean   leftRatio;
    private  boolean   topRatio;
    private  boolean   rightRatio;
    private  boolean   bottomRatio;

    public void addLayoutComponent(String p, Component c) {
        positions.addElement(p);
        components.addElement(c);
    }


    public void removeLayoutComponent(Component c) {

        int i = components.indexOf(c);
        if(i != -1) {
            positions.removeElementAt(i);
            components.removeElementAt(i);
        }
    }


    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0,0);
        int ncomponents = parent.getComponentCount();
        Insets  insets = parent.getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;

        int maxW = dim.width;
        int maxH = dim.height;
        for(int i = 0; i < ncomponents; i++) {
            Component  com = parent.getComponent(i);
            if(com.isVisible() == false)
                continue;
            int  w = com.getPreferredSize().width + dim.width;
            int  h = com.getPreferredSize().height + dim.height;
            if( w > maxW )
                maxW = w;
            if( h > maxH )
                maxH = h;
        }
        //System.out.println("prefer Size: width="+maxW+"\t\theight="+maxH);
        return  new Dimension(maxW, maxH);
    }


    public Dimension minimumLayoutSize(Container target) {
        return  target.getSize();
    }


    public void layoutContainer(Container target) {
        Insets insets = target.getInsets();
        int    ncomponents = target.getComponentCount();
        Dimension d = target.getSize();
        d.width  -= insets.left + insets.right;
        d.height  -= insets.top + insets.bottom;
        int  startX =0, startY =0, endX =0, endY =0;
        int  left = 0, top =0, right = 0, bottom =0;
        for(int i=0; i< ncomponents; i++) {
            Component comp = target.getComponent(i);
         StringTokenizer token = new StringTokenizer((String)positions.elementAt(i), ", \t");
            String  leftSt = token.nextToken();
            if(leftSt.endsWith("%")) {
                leftRatio = true;
                left  = Integer.parseInt(leftSt.substring(0, leftSt.length()-1));
            } else {
                left  = Integer.parseInt(leftSt);
            }

            String  topSt = token.nextToken();
            if(topSt.endsWith("%")) {
                topRatio = true;
                top  = Integer.parseInt(topSt.substring(0, topSt.length()-1));
            } else {
                top  = Integer.parseInt(topSt);
            }

            String  rightSt = token.nextToken();
            if(rightSt.endsWith("%")) {
                rightRatio = true;
                right  = Integer.parseInt(rightSt.substring(0, rightSt.length()-1));
            } else {
                right  = Integer.parseInt(rightSt);
            }

            String  bottomSt = token.nextToken();
            if(bottomSt.endsWith("%")) {
                bottomRatio = true;
                bottom  = Integer.parseInt(bottomSt.substring(0, bottomSt.length()-1));
            } else {
                bottom  = Integer.parseInt(bottomSt);
            }

            if(leftRatio)
                startX = (d.width * left)/100;
            else
                startX = left;

            if(topRatio)
                startY = (d.height * top)/100;
            else
                startY = top;

            if(rightRatio)
                endX = (d.width * right)/100;
            else
                endX = right;

            if(bottomRatio)
                endY = (d.height * bottom)/100;
            else
                endY = bottom;

            if(startX > endX) {
                int temp = startX;
                startX = endX;
                endX = temp;
            }
            if(startY > endY) {
                int temp = startY;
                startY = endY;
                endY = temp;
            }
            int  w = endX - startX;
            int  h = endY - startY;
            comp.setBounds(startX+insets.left, startY+insets.top, w, h);
            topRatio = false;
            leftRatio = false;
            rightRatio = false;
            bottomRatio = false;
        }
    }

    public String toString() {
        return getClass().getName();
    }
}

'JAVA 6.0 > GUI (AWT, SWING)' 카테고리의 다른 글

JFreeChart Package Documentation  (0) 2009.11.15
JFreeChart Package File  (0) 2009.11.15
JFreeChart 사용하기 (HistogramChart)  (0) 2009.11.15
JFreeChart 사용하기 (ScatterPlot)  (0) 2009.11.15
JFreeChart 사용하기 (LineChart)  (0) 2009.11.15
Posted by TIMEC
2009. 11. 15. 19:50


package JFreeChart;

import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class HistogramChart{
 private XYSeries series = null;
 private XYDataset dataset = null;
 private JFreeChart chart = null;
 private TextTitle subTitle = null;
 
 public HistogramChart(){
  series = new XYSeries("Users");
  series.add(2000, 100.0);
  series.add(2001, 111.0);
  series.add(2002, 120.0);
  series.add(2003, 140.0);
 }
 public ChartPanel getHistogramChart_HistogramChart(){
  ChartPanel chartPanel_HistogramChart = null;
  dataset = new XYSeriesCollection(series);
  chart = ChartFactory.createXYBarChart("Response Time  Monitoring", "Var", false, "Frequence", (IntervalXYDataset) dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL, true, true, false);
  subTitle = new TextTitle("Thread Test");
  chart.setBackgroundPaint(Color.WHITE);
  chart.addSubtitle(subTitle);
  chartPanel_HistogramChart = new ChartPanel(chart);
  return chartPanel_HistogramChart;
 }
}

Posted by TIMEC
2009. 11. 15. 19:49

package JFreeChart;

import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ScatterPlot {
 private XYSeries series = null;
 private XYDataset dataset = null;
 private JFreeChart chart = null;
 private TextTitle subTitle = null;

 
 public ScatterPlot(){
  series = new XYSeries("Users");
  series.add(2000, 100.0);
  series.add(2001, 111.0);
  series.add(2002, 120.0);
  series.add(2002, 130.0);
  series.add(2001, 110.0);
  series.add(2002, 150.0);
  series.add(2001, 120.0);
  series.add(2003, 170.0);
  series.add(2001, 160.0);
  series.add(2002, 120.0);
  
    }
 public ChartPanel getchartPanel_scatterPlot(){
  ChartPanel chartPanel_scatterPlot = null;
  dataset = new XYSeriesCollection(series);
  chart = ChartFactory.createScatterPlot("JDBC & SQL Monitoring", "Var",  "Frequence", dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL, true, true, false);
  subTitle = new TextTitle("ClientRequirement Test");
  chart.setBackgroundPaint(Color.WHITE);
  chart.addSubtitle(subTitle);
  chartPanel_scatterPlot = new ChartPanel(chart);
  return chartPanel_scatterPlot;
 }
}

 

Posted by TIMEC
2009. 11. 15. 19:48


package JFreeChart;

import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LineChart {
 private XYSeries series = null;
 private XYDataset dataset = null;
 private JFreeChart chart = null;
 private TextTitle subTitle = null;
 
 public LineChart(){
  series = new XYSeries("Users");
  series.add(2000, 100.0);
  series.add(2001, 111.0);
  series.add(2002, 120.0);
  series.add(2003, 140.0);
 }
 public ChartPanel getchartPanel_lineChart(){
  ChartPanel chartPanel_lineChart = null;
  dataset = new XYSeriesCollection(series);
  chart = ChartFactory.createXYLineChart("WebServer Performance Monitoring", "Var",  "Frequence", dataset, org.jfree.chart.plot.PlotOrientation.VERTICAL, true, true, false);
  subTitle = new TextTitle("Heap Memory Usage Test");
  chart.setBackgroundPaint(Color.WHITE);
  chart.addSubtitle(subTitle);
  chartPanel_lineChart = new ChartPanel(chart);
  return chartPanel_lineChart;
 }
 
}

Posted by TIMEC